Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlight Selected Tab in ASP.Net Menu

Tags:

css

asp.net

I am trying to hightlight with different color the selected tab in ASP.NET menu. It seems simple to do this but first I am not able to make it to work, and second I cannot find a good working example so far.

ASP/HTML

<div class="clear hideSkiplink">
                <asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="False"
                    IncludeStyleBlock="false" Orientation="Horizontal">

                    <Items>
                        <asp:MenuItem NavigateUrl="~/ReadMe.aspx" Text="Read Me" />
                        <asp:MenuItem NavigateUrl="~/Summary.aspx" Text="Summary" />
                        <asp:MenuItem NavigateUrl="~/Detail.aspx" Text="Detail" />
                    </Items>
                </asp:Menu>
  </div>

CSS

div.hideSkiplink
{
    background-color:#3a4f63;
    width:100%;
}

div.menu
{
    padding: 4px 0px 4px 8px;
}

div.menu ul
{
    list-style: none;
    margin: 0px;
    padding: 0px;
    width: auto;

}

div.menu ul li a, div.menu ul li a:visited
{
    background-color: #465c71;
    border: 1px #4e667d solid;
    color: #dde4ec;
    display: block;
    line-height: 1.35em;
    padding: 4px 20px;
    text-decoration: none;
    white-space: nowrap;
}

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

div.menu ul li a:active
{
    background-color: #465c71;

    color: #cfdbe6;
    text-decoration: none;
}
like image 448
moe Avatar asked Dec 21 '22 10:12

moe


1 Answers

This is a Working solution: *Script*

<script language="javascript" type="text/javascript">

   $(function () {
        // this will get the full URL at the address bar
        var url = window.location.href;

        // passes on every "a" tag 
        $("#cssmenu a").each(function () {
            // checks if its the same on the address bar
            if (url == (this.href)) {
                $(this).closest("li").addClass("active");
            }
        });
        $("#header a").each(function () {
            // checks if its the same on the address bar
            if (url == (this.href)) {
                $(this).closest("li").addClass("active");
            }
        });

    });
</script>

Markup

List items with div id ="cssmenu"

<div id='cssmenu'>
    <ul>
       <li><a href="../Admin/dashboard.aspx"><span>Dashboard</span></a></li>
       <li><a href="../Admin/Report.aspx"><span>Reports</span></a></li>
       <li><a href="../Admin/Shop.aspx"><span>Shop</span></a></li>
       <li><a href="../Admin/system.aspx"><span>System</span></a></li>
    </ul>
</div>

StyleSheet

#cssmenu ul {
    list-style-type: none;
    width: auto;
    position: relative;
    display: block;
    height: 33px;
    font-size: .6em;
    background: url(images/bg.png) repeat-x top left;
    font-family: Verdana,Helvetica,Arial,sans-serif;
    border: 1px solid #000;
    margin: 0;
    padding: 0;
}

#cssmenu li {
    display: block;
    float: left;
    margin: 0;
    padding: 0;
}

#cssmenu li a {
    float: left;
    color: #A79787;
    text-decoration: none;
    height: 24px;
    padding: 9px 15px 0;
    font-weight: normal;
}

#cssmenu li a:hover,
#cssmenu .active {
    color: #fff;
    background: url(images/bg.png) repeat-x top left;
    text-decoration: none;
}

#cssmenu .active a {
    color: #fff;
    font-weight: 700;
}

#cssmenu ul { background-color: #629600 }
#cssmenu li a:hover,
#cssmenu li.active { background-color: #7AB900 }
like image 73
Sakthivel Avatar answered Jan 03 '23 20:01

Sakthivel