Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build a Menu from the web.sitemap file in ASP.NET

Tags:

asp.net

I have started a new ASP.NET 4 WebForm application. By default, the Site.Master file contains the following menu:

<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false"   IncludeStyleBlock="false" Orientation="Horizontal">
  <Items>
    <asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home"/>
    <asp:MenuItem NavigateUrl="~/About.aspx" Text="About"/>
  </Items>
</asp:Menu>

This menu contains two blocks: "Home" and "About". I like this structure. However, I want to populate the NavigationMenu based upon the contents of my Web.sitemap file. At this time, this file looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
  <siteMapNode>
    <siteMapNode url="/Default.aspx" title="Home"  description=""></siteMapNode>
    <siteMapNode url="/Products/List.aspx" title="Products" description=""></siteMapNode>
  </siteMapNode>
</siteMap>

I changed the NavigationMenu code to look like the following:

<asp:SiteMapDataSource ID="mySiteMapDataSource" runat="server" />
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" DataSourceID="mySiteMapDataSource" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal" />

My problem is, this approach creates a small block that represents the menu. When the user hovers over this, two sub-menu items appear "Home" and "Products". Oddly, the web.sitemap file only alows for one siteMapNode as the child of the siteMap element. How do I make it such that "Home" and "Products" appear in the same way that "Home" and "About" did, while giving me the flexibility of using the sitemap?

Thank you!

like image 229
Villager Avatar asked Oct 19 '10 14:10

Villager


2 Answers

For me ShowStartingNode="false" worked better.

like image 133
MindLoggedOut Avatar answered Oct 17 '22 22:10

MindLoggedOut


You can use the StartingNodeOffset property of SitemapDataSource control to tune what part of the sitemap hierarchy is exposed. In your case, if you set it to 1, all should be fine.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sitemapdatasource.startingnodeoffset.aspx

like image 2
Slavo Avatar answered Oct 17 '22 21:10

Slavo