简体   繁体   中英

Styling current page in ASP.NET menu

I have master page with menu bar and a few nested pages. What is best practice to highlight selected menu item? For example:

[ Profile ] [Forum] [ Statistics ]


Statistics

blblalbla

This is what I use:

    //select menu item with matching NavigateUrl property
    foreach (MenuItem ParentMenu in menu.Items)
    {
        if (ParentMenu.NavigateUrl.ToLower() == Page.AppRelativeVirtualPath.ToLower())
        {
            ParentMenu.Selected = true;
        }
        else
        {
            foreach (MenuItem childMenu in ParentMenu.ChildItems)
            {
                if (childMenu.NavigateUrl.ToLower() == Page.AppRelativeVirtualPath.ToLower())
                {
                    childMenu.Selected = true;
                }
            }
        }
    }

The best way would be to put the menu in a control. You can then have properties such as SelectedMenu which render the style of the selected menu item.

You can read about user controls here:

http://msdn.microsoft.com/en-us/library/fb3w5b53.aspx

They can be a little tricky at first, but once you have got the hang of them they will be very useful to you.

you can highlight the menuitem based on the current url.

Use StaticSelectedStyle and DynamicSelectedStyle properties

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM