开发者

ASP.NET Menu Problem

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_Load event 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:

foreach (var menuItem in Menu1.Items)  
{  
    if (Request.Path == menuItem.NavigateUrl)
    {
        menuItem.NavigateUrl = "";
        // Add any CSS modifications here
        break;
    }
}

To be absolutely sure, you could compare the physical paths:

foreach (var menuItem in Menu1.Items)  
{  
    if (Request.PhysicalPath == Server.MapPath(menuItem.NavigateUrl))
    {
        menuItem.NavigateUrl = "";
        // Add any CSS modifications here
        break;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜