I have a list of asp.net menu items:
<asp:Menu ID="Menu1" runat="server" OnLoad="Menu1_Load">
<Items>
<asp:MenuItem Text="Create Member" Value="Create Member" NavigateUrl="~/create-member.aspx"></asp:MenuItem>
<asp:MenuItem Text="Edit Member" Value="Edit Member" NavigateUrl="~/edit-member.aspx"></asp:MenuItem>
<asp:MenuItem Text="Result Export" Value="Result Export" NavigateUrl="~/result-export.aspx"></asp:MenuItem>
...
...
</Items>
</asp:Menu>
How can I highlight and remove hyperlink for the currently selected MenuItem?
This is the modified version of Josh’s answer (we don’t need to set the navigateUrl):
foreach (MenuItem menuItem in Menu1.Items)
{
if (Request.PhysicalPath == Server.MapPath(menuItem.NavigateUrl))
{
menuItem.Selected = true;
menuItem.Selectable = false;
break;
}
}
If you are looking to do this server-side based on the page being visited, you’ll just need to make that evaluation, then find the related item and set the NavigateUrl property to “”. Depending on your CSS settings you might also need to assign a specific class to modify the appearance and give the user some visual feedback of their location.
Edit
Probably the best way to do this is to use your
Menu1_Loadevent to test the page URL against the NavigateUrl. If there’s a match, remove the NavigateUrl setting. If you weren’t using the~to establish the root, I might use:To be absolutely sure, you could compare the physical paths: