我在ASP.NET 4中使用此代码来创建菜单并添加StaticSelectedStyle& DynamicSelectedStyle用于当前页面的高亮显示菜单项:
<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>
但是对于动态页面,突出显示当前项目不起作用
怎么了?
当前页面的高亮菜单项有更好的方法吗?
答案 0 :(得分:2)
您需要手动设置Selected MenuItem
NavigationMenu.Items(i).Selected = True
我创建了一个功能,使突出显示更容易。
SelectMenuByValue("Value2", NavigationMenu)
将MenuItem和Menu控件实例的值作为参数。
<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>
代码隐藏:
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个或更多MenuItem,因此如果选择了其中一个子菜单,则parentMenu也不能被突出显示。你可以在jQuery中做到这一点。
如果您有2个或更多具有相同值的menuItem,则会选择第一个。