1

I know this has been asked, and answered before, but I still can't get it to work. And I've boiled it down to this. If I specify a NavigateURL in a MenuItem, it does not work. If I don't, the menu item changes its style as expected when clicked. However, it is completely useless as a navigation menu, since it no longer takes you anywhere! :)

So to clarify, this works - selected item is hilighted as per specified style:

    <asp:menu id="NavigationMenu" staticdisplaylevels="1" orientation="Horizontal" runat="server">
        <staticselectedstyle backcolor="LightBlue" borderstyle="Solid" bordercolor="Black" borderwidth="1"/>
        <items>
            <asp:MenuItem Text="Home" Value="Home" />
            <asp:MenuItem Text="Software" Value="Software" />
        </items>
    </asp:menu>

And this doesn't work - only change is adding NavigateURLs:

    <asp:menu id="NavigationMenu" staticdisplaylevels="1" orientation="Horizontal" runat="server">
        <staticselectedstyle backcolor="LightBlue" borderstyle="Solid" bordercolor="Black" borderwidth="1"/>
        <items>
            <asp:MenuItem NavigateUrl="/Default.aspx" Text="Home" Value="Home" />
            <asp:MenuItem NavigateUrl="/Software.aspx" Text="Software" Value="Software" />
        </items>
    </asp:menu>

In this other post, the OP is using NavigateURLs, and has accepted the answer about StaticSelectedStyle. I don't get it.

I would like to understand how to keep the StaticSelectedStyle working, and use NavigateURLs, at the same time.

I should add that the menu is in the master page. The pages being navigated to use this master page.

Thanks! -Sandra

EDIT:

Based on my reading on this topic, I think this doesn't work because the Menu control only knows where it is on a Postback. But if your menu item takes you to some other page, it is no longer a postback, and the menu control is loaded afresh and does not know which item was clicked.

Community
  • 1
  • 1
Sandra
  • 608
  • 2
  • 11
  • 23

1 Answers1

0

Try adding some code to the Page_Load section of your master file to bounce the page name from the url against the NaviagteUrl from your menu item. Here:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    For Each item As MenuItem In NavigationMenu.Items
        If Request.Url.AbsoluteUri.ToLower().Contains(Page.ResolveUrl(item.NavigateUrl.ToLower())) Then
            item.Selected = True
        End If
    Next

End Sub

That is in VB.NET. It works, I tested it using your markup from above. I hope you can figure out how to fit that into your code.

Taylor Brown
  • 1,689
  • 2
  • 17
  • 33