ASP.NET 4:当前页面高亮显示菜单项 [英] ASP.NET 4 : Highlight menu item for current page

查看:114
本文介绍了ASP.NET 4:当前页面高亮显示菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用ASP.NET 4本code创建菜单,并添加StaticSelectedStyle&安培; DynamicSelectedStyle为高亮显示菜单项为当前页面:

I use this code in ASP.NET 4 for creating menu and add StaticSelectedStyle & DynamicSelectedStyle for highlight menu item for current page :

  <asp:Menu ID="Menu1" runat="server" BackColor="#E3EAEB" 
      DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="0.8em" 
      ForeColor="#666666" Orientation="Horizontal" RenderingMode="List" 
      StaticSubMenuIndent="10px">
      <DynamicSelectedStyle backcolor="LightBlue"
  borderstyle="Solid"
  bordercolor="Black"
  borderwidth="1"/>
      <StaticSelectedStyle backcolor="LightBlue"
  borderstyle="Solid"
  bordercolor="Black"
  borderwidth="1"/>

  </asp:Menu>

但对于动态页面选择当前项目是行不通的。

什么是错了吗?

是否有亮点菜单项更好地为当前页面?

But for dynamic pages highlight current item is not working.
What is wrong?
Is there better way for highlight menu item for current page?

推荐答案

您需要设置菜单项选择手动

You need to set the Selected MenuItem Manually

NavigationMenu.Items(i).Selected = True

我创建了一个功能,使突出更加容易。

I've created a function to make highlighting easier.

SelectMenuByValue("Value2", NavigationMenu)

它采用的菜单项的价值和菜单控件实例作为参数。<​​/ P>

It takes the Value of the MenuItem and the Menu control instance as Parameters.

<asp:Menu ID="NavigationMenu" runat="server">
    <Items>
        <asp:MenuItem Text="Parent1" Value="ParentValue">
            <asp:MenuItem Text="SubMenu1" Value="Value1" NavigateUrl="~/Page1.aspx" />
            <asp:MenuItem Text="SubMenu2" Value="Value2" NavigateUrl="~/Page2.aspx" />
            <asp:MenuItem Text="SubMenu3" Value="Value3" NavigateUrl="~/Page3.aspx" />
        </asp:MenuItem>
    </Items>
</asp:Menu>

code-背后:

code-behind:

Public Sub SelectMenuByValue(ByVal sValue As String, ByVal NavigationMenu As Menu)
    Dim iMenuCount As Integer = NavigationMenu.Items.Count - 1
    For i As Integer = 0 To iMenuCount
        Dim menuItem As MenuItem = NavigationMenu.Items(i)
        If menuItem.Value = sValue Then
            If menuItem.Enabled AndAlso menuItem.Selectable Then menuItem.Selected = True
            Exit For
        End If
        If CheckSelectSubMenu(menuItem, sValue) Then Exit For
    Next
End Sub

Private Function CheckSelectSubMenu(ByVal menuItem As MenuItem, ByVal sValue As String) As Boolean
    CheckSelectSubMenu = False
    Dim iMenuCount As Integer = menuItem.ChildItems.Count - 1
    For i As Integer = 0 To iMenuCount
        Dim subMenuItem As MenuItem = menuItem.ChildItems(i)
        If subMenuItem.Value = sValue Then
            CheckSelectSubMenu = True
            If subMenuItem.Enabled AndAlso subMenuItem.Selectable Then subMenuItem.Selected = True
            Exit For
        End If
        If CheckSelectSubMenu(subMenuItem, sValue) Then
            CheckSelectSubMenu = True
            Exit For
        End If
    Next
End Function

限制:


  • 有没有办法选择2个或更多菜单项的一次,所以parentMenu不能也亮显,如果选择其子菜单中的一个。同时你可以在jQuery的做到这一点,但。

  • There is no way to select 2 or more MenuItem at once, so the parentMenu cannot be higlighted also if one of its submenu is selected. well you can do this in jQuery though.

如果你有2个或更多的菜单项具有相同的,第一个会被选中。

If you have 2 or more menuItems that has the same Value, the first one will be selected.

这篇关于ASP.NET 4:当前页面高亮显示菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆