Styling current page in ASP.NET menu
I have maste开发者_运维知识库r 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
精彩评论