本文介绍了突出显示ASP.NET Web应用程序中的菜单栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Visual Studio 2010,当我添加一个新的项目,VS给我默认的CSS和UI主题。我有6个aspx页面。现在每当用户进入某个特定页面时,我想要该页面的菜单栏突出显示,以便用户知道他/她是哪个页面。

I have a Visual Studio 2010 and when I added a NEW project, VS gives me default css and UI themes. I have 6 aspx pages. Now whenever user is going into some specific page, I want the menu bar of that page to get highlighted so that user will know which page he/she is.

一个名为:CommSetup.aspx的页面。在page_load中我写了以下代码:

I have a page called: CommSetup.aspx. In the page_load I have written this code:

在主页中,我改变了这个:

And in the master page I changed this:



protected void Page_Load(object sender, EventArgs e)
    {
        foreach (MenuItem item in NavigationMenu.Items)
        {
            var navigateUrlParams = item.NavigateUrl.Split('/');
            if (Request.Url.AbsoluteUri.IndexOf(navigateUrlParams[navigateUrlParams.Length - 1]) != -1)
            {
                item.Selected = true;
            }
        }
    }





 <body runat ="server" clientidmode ="Static" id = "MasterBody">
    <form runat="server">......
<asp:Menu ID="NavigationMenu" runat="server"  StaticSelectedStyle-CssClass ="Selected"      CssClass="menu" .....

这是我在Site.css中添加的:

This is what I added in Site.css:

div.menu ul li a.Selected
{
    background-color: #bfcbd6;
    color: #465c71;
    text-decoration: none;
}


推荐答案

您要求使用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 */
}

这篇关于突出显示ASP.NET Web应用程序中的菜单栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:08