简体   繁体   English

ASP.NET 菜单中的当前页面样式

[英]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 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.然后,您可以使用SelectedMenu等属性来呈现所选菜单项的样式。

You can read about user controls here:您可以在此处阅读有关用户控件的信息:

http://msdn.microsoft.com/en-us/library/fb3w5b53.aspx 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.您可以根据当前的 url 突出显示菜单项。

Use StaticSelectedStyle and DynamicSelectedStyle properties使用StaticSelectedStyleDynamicSelectedStyle属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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