Search code examples
asp.netsitemapaspmenu-control

Using ASP.net Menu Control with a sitemap


I have following sitemap defined:

<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
  <siteMapNode url="" title="Root" roles="*">
    <siteMapNode url="~/Default.aspx" title="Home" roles="*" />
    <siteMapNode url="~/ProjectList.aspx" title="Projects" roles="*">
      <siteMapNode url="~/ProjectOverview.aspx" title="Project Overview"  roles="*" />
      <siteMapNode url="~/ProjectViewCalls.aspx" title="View Calls" roles="*" />
    </siteMapNode>
    <siteMapNode url="~/Configuration.aspx" title="Configuration" roles="Administrator" />
    <siteMapNode url="~/YourAccount.aspx" title="Your Account" roles="Administrator" />
    <siteMapNode url="~/Logout.aspx" title="Logout" roles="*" />
  </siteMapNode>
</siteMap>

I need this to display in my menu control as: Home | Projects | Configuration | Your Account | Logout.

This is working correctly however when i navigate to the pages ProjectOverview and ProjectViewCalls, I lose the selected class="level1 selected" attribute of the list item. I want to be able to indicate what area of the site the user is currently in.

Is this possible?


Solution

  • Not sure if this is what you're looking for, but here is an easy way to do it. Add a MenuItemDataBound event to the menu control, then in the event use this code:

            if(e.Item.Selected)
            {
                if(e.Item.Parent != null && e.Item.Parent.Selectable)
                {
                    e.Item.Parent.Selected = true;
                }
            }
    

    If you do this, the current menu item will not have the selected style, so it might mess up your pop-out sub menu.

    If the child nodes aren't being displayed at all, you could try binding something like this on MenuDataBound:

    var myMenu = (Menu) sender;
    var currentNode = SiteMap.Provider.FindSiteMapNode(HttpContext.Current);
    if (currentNode != null)
    {
        var parentMenuItem = myMenu.FindItem("Root/" + currentNode.ParentNode.Title);
        if (parentMenuItem != null && parentMenuItem.Selectable)
        {
            parentMenuItem.Selected = true;
        }
    }
    

    Another option would be to ditch the default menu script and use something like Superfish instead.