简体   繁体   English

StaticSelectedStyle-CssClass不起作用

[英]StaticSelectedStyle-CssClass Not Working

I am building an ASP.NET 4.0 web application in which I have a menu control as follows: 我正在构建一个ASP.NET 4.0 Web应用程序,其中我有一个菜单控件,如下所示:

   #menu {
        width: 940px;
        height: 36px;
        margin: 0 auto;
        padding: 0;
    }

    #menu ul {
        margin: 0px 0px 0px 10px;
        padding: 0;
        list-style: none;
        line-height: normal;
    }

    #menu li {
        float: left;
    }

    #menu a {
        display: block;
        height: 26px;
        margin-right: 2px;
        margin-bottom: 10px;
        padding: 10px 20px 0px 20px;
        text-decoration: none;
        text-align: center;
        text-transform: uppercase;
        font-family: Arial, Helvetica, sans-serif;
        font-size: 11px;
        font-weight: bold;
        color: #FFFFFF;
        border: none;
    }

    #menu a:hover, .selectedMenuItem {
        background: #FFFFFF;
        text-decoration: none;
        color: #333333;
    }


    <asp:Menu ID="menu" runat="server" StaticSelectedStyle-CssClass="selectedMenuItem">
    <Items>
        <asp:MenuItem Text="Home" Selected="true" NavigateUrl="~/Home.aspx"></asp:MenuItem>
        <asp:MenuItem Text="About Us" NavigateUrl="~/AboutUs.aspx"></asp:MenuItem>
        <asp:MenuItem Text="Services" NavigateUrl="~/Services.aspx"></asp:MenuItem>
        <asp:MenuItem Text="Contact" NavigateUrl="~/Contact.aspx"></asp:MenuItem>
    </Items>
    </asp:Menu>

I want the selected menu item to be styled according to the css class selectedMenuItem but for some reason that doesn't happen. 我希望根据css类selectedMenuItem设置所选菜单项的样式,但由于某些原因不会发生。 How can I fix this problem? 我该如何解决这个问题?

I got the same problem and tried to find out what happened. 我遇到了同样的问题,并试图找出发生了什么。 In the source-code of thje html-page I found this: 在html-page的源代码中我发现了这个:

<div class="menu" id="NavigationMenu">
    <ul class="level1">
        <li><a class="level1" href="../Default.aspx">Start</a></li>
        <!-- more li items with the same class -->
    </ul>

I expected my CssClass name but found level1 instead. 我期待我的CssClass名称,但找到了level1。 If I changed my css to: 如果我把我的CSS更改为:

#NavigationMenu .level1 li
{
    padding: 10px;
}

it worked. 有效。 Probably not the correct way to do it, but it's a work-around of the problem at hand. 可能不是正确的方法,但这是解决手头问题的方法。 Probably not suitable on a production server, unfortunately 遗憾的是,可能不适合生产服务器

1- Define .selectedMenuItem class as below, independent of ID (#menu) 1-定义.selectedMenuItem类,如下所示,与ID无关(#menu)

.selectedMenuItem {
        background: #FFFFFF;
        text-decoration: none;
        color: #333333;
}

2- Use jQuery's addClass method, bind that method to the click event of menu items. 2-使用jQuery的addClass方法,将该方法绑定到菜单项的click事件。

http://api.jquery.com/addClass/ http://api.jquery.com/addClass/

You can manage this from code behind on the Page_Load event of the Master file. 您可以从主文件的Page_Load事件后面的代码进行管理。 For example, you can use the Request.Path method to get the current page Path and then use a switch or if statement to manually add the style to the corresponding MenuItem . 例如,您可以使用Request.Path方法获取当前页面Path,然后使用switch或if语句手动将样式添加到相应的MenuItem

simply add this css in style sheet to show active tab with backgroung image or also add background active color no need to made any cahnge in menu control and add any static or dynamic style in menu . 只需在样式表中添加此css即可显示带有背景图像的活动标签,或者还可以添加背景活动颜色,无需在菜单控件中进行任何操作,并在菜单中添加任何静态或动态样式。

.menu a.static.selected
{
background: url("../images/bg.jpg") repeat scroll 0 0;
color: white;``
text-decoration: none; 
}
.menu li a.dynamic.selected
{

background: url("../images/bg.jpg") repeat scroll 0 0;
color: white;
text-decoration: none; 
}

你有没有找到你的borwser源来确保ASP.NET没有动态重命名你的菜单控件ID值?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM