且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

突出显示ASP.NET Web应用程序中的菜单栏

更新时间:2023-02-16 16:13:05

您要求使用CSS:

http://hicksdesign.co.uk/journal/highlighting-current-page-with-css

您应该能够实现在控件上使用静态ID命名(而不是让ASP.NET为每个控件分配ID值)。

You should be able to achieve that using static ID naming on your controls (instead of letting ASP.NET assign the ID value to each control).

编辑:
要想使用母版页,请将母版页中的< body> 标记更改为:

<body runat="server" clientidmode="Static" id="MasterBody">

然后在每个页面的Page_Load中,您可以覆盖每个页面的ID我的例子是类型 SiteMaster ):

Then in the Page_Load of each page, you can overwrite the id for each page (the master page in my example is of type SiteMaster):

protected void Page_Load(object sender, EventArgs e)
{
    Control c = Page.Master.FindControl("MasterBody");
    if (c != null)
    {
        c.ID = "Page1";
    }
}



更新(2):



我试过运行Farzin的例子,它似乎不工作,所以这里是我能够验证为我工作(你不需要 Page_Load ):

Site.master

<asp:Menu ID="NavigationMenu" ...>
    <StaticSelectedStyle CssClass="selected" />
    ...
</asp:Menu>

Site.master.cs

protected void Page_Load(object sender, EventArgs e)
{
    foreach (MenuItem item in NavigationMenu.Items)
    {
        item.Selected = Page.ResolveUrl(item.NavigateUrl).ToLowerInvariant() == Request.Path.ToLowerInvariant();
    }
}

Styles / Site.css

div.menu ul li a.selected
{
    /* put your style definition here */
}