开发者

Using ASP.net Menu Control with a sitemap

I 开发者_如何学Pythonhave 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?


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.


I have written a detailed article for this at codeproject ( http://www.codeproject.com/Articles/669717/How-to-correctly-use-sitemap-for-top-left-menus ) and here ( http://mangeshdevikar.enziq.com/how-to-correctly-use-sitemap-for-topleft-menus/ ) . Hope it helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜