You need to install Selected MenuItem manually
NavigationMenu.Items(i).Selected = True
I created a function to make selection easier.
SelectMenuByValue("Value2", NavigationMenu)
Parameters are used as parameters for the MenuItem parameter and the Menu control.
<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>
background code:
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
Limitations:
It is not possible to select 2 or more MenuItem at once, therefore parentMenu cannot be highlighted either if one of its submenus is selected. but you can do it in jQuery though.
If you have 2 or more menu items that have the same Value , the first one will be selected.
source share