0

I am using a master for my menu items. Since I am using master page, I am confused how to highlight active menu item. Can anyone help me out. Since I have 4 pages, I tried this below code in all page load event in 4 pages and didn't work.

if (!IsPostBack)
        {
            Menu M = (Menu)this.Master.FindControl("menu");
            foreach (MenuItem item in M.Items)
            {
                if (item.Text == "update")
                {
                    item.Selected = true;
                }
            }
        }

 <div id="menu">
                <ul>
                    <li>
                        <asp:LinkButton ID="update"  runat="server"  OnClick="update_Click">Update</asp:LinkButton></li>
                    <li>
                        <asp:LinkButton ID="bulk" runat="server" OnClick="bulkupdate_Click">Bulk</asp:LinkButton></li>
                    <li>
                        <asp:LinkButton ID="profiles" runat="server" OnClick="userprofiles_Click">Profiles</asp:LinkButton></li>
                    <li>
                        <asp:LinkButton ID="help" runat="server" OnClick="help_Click">Help</asp:LinkButton></li>
                </ul>
            </div>

#menu ul {
margin: 0;
padding: 7px 6px 0;
background: #b6b6b6 url('/Images/Overlay.png') repeat-x 0 -110px;
line-height: 100%;
border-radius: 1em;
font: normal 0.5333333333333333em Arial, Helvetica, sans-serif;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
-webkit-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
-moz-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
width 100%;
}
#menu li {
margin: 0 5px;
padding: 0 0 8px;
float: left;
position: relative;
list-style: none;
}
#menu a,
#menu a:link {
font-weight: bold;
font-size: 16px;
color: #444444;
text-decoration: none;
display: block;
padding: 8px 20px;
margin: 0;
border-radius: 5px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.3);
}
#menu a:hover {
background: #000;
color: #fff;
}
#menu .active a,
#menu li:hover > a {
background: #bdbdbd url('/Images/Overlay.png') repeat-x 0 -40px;
background: #666666 url('/Images/Overlay.png') repeat-x 0 -40px;
color: #444;
border-top: solid 1px  #f8f8f8;
-webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
-moz-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
text-shadow: 0 1px 0 #ffffff;
}
#menu ul ul li:hover a,
#menu li:hover li a {
background: none;
border: none;
color: #666;
-webkit-box-shadow: none;
-moz-box-shadow: none;
}
#menu ul ul a:hover {
background: #7d7d7d url('/Images/Overlay.png') repeat-x 0 -100px !important;
color: #fff !important;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
}
#menu li:hover > ul {
display: block;
}
#menu ul ul {
display: none;
margin: 0;
padding: 0;
width: 185px;
position: absolute;
top: 40px;
left: 0;
background: url('/Images/Overlay.png') repeat-x 0 0;
border: solid 1px #b4b4b4;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
-webkit-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
-moz-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}
#menu ul ul li {
float: none;
margin: 0;
padding: 3px;
}
#menu ul ul a,
#menu ul ul a:link {
font-weight: normal;
font-size: 12px;
}
#menu ul:after {
content: '.';
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
* html #menu ul {
height: 1%;
}
  • This is answered in another SO topic: http://stackoverflow.com/questions/7067944/asp-net-highlight-menu-item-of-current-page – Craig Moore Jan 08 '14 at 08:07

5 Answers5

0

Based on the Page you are in, cant you set the active item using Jquery in document.ready ? Not a Good practise though.. this might require a code change if menu items increase.

Lakshman
  • 65
  • 9
  • You mean to put jquery in each pages seperately. –  Jan 08 '14 at 08:02
  • yes, but make sure you have some mapping between your page and the menu item name so that you wont end up hardcoding. You can place a function in one of the script pages and call this method from doc.ready of your content page. – Lakshman Jan 08 '14 at 08:05
0

use CSS for active menu like

active{    
        background-color: #CDE3F7;    
        border-radius: 5px;    
        color: #000000;   
        text-decoration: none;   
      }
0

use asp.net menu control like below...

<asp:Menu Width="100%" ItemWrap="true" ID="SelectMenu" runat="server"                DynamicHorizontalOffset="2"
                    StaticSubMenuIndent="10px" CssClass="menubar" Orientation="Horizontal">
                    <DynamicHoverStyle CssClass="menuitem-hover" />
                    <DynamicMenuItemStyle HorizontalPadding="2px" CssClass="menuitem" />
                    <DynamicMenuStyle BackColor="#F7F6F3" CssClass="IE8Fix" />
                    <DynamicSelectedStyle CssClass="menuitemsel" />
                    <StaticHoverStyle ForeColor="Yellow" />
                    <StaticMenuItemStyle HorizontalPadding="15px" VerticalPadding="0px"  CssClass="menu" />
                    <StaticSelectedStyle CssClass="menuitemselstatic" />
                    <Items>
</items>
</asp:Menu>
VIPUL PARMAR
  • 280
  • 2
  • 4
  • 29
0

@user3003821 - check this out Dirty, but works

MasterPage HTML :

 <div id="MenuHolderDiv">
                <ul>
                    <li class="menuLst"><a href="#" id="MenuItem1" onclick="redirectOnClick(this)">MenuItem1</a></li>

                    <li class="menuLst"><a href="#" id="MenuItem2"  onclick="redirectOnClick(this)">MenuItem2</a></li>

                    <li class="menuLst"><a href="#" id="MenuItem3"  onclick="redirectOnClick(this)">MenuItem3</a></li>

                    <li class="menuLst"><a href="#" id="MenuItem4"  onclick="redirectOnClick(this)">MenuItem4</a></li>

                </ul>
            </div>

    </div>

Master Page: Script:

 <script type="text/javascript">
        $(document).ready(function () {
            var pageName = getPageName(window.location.href);
            $("#" + pageName).addClass("active");
            $("#" + pageName).parent().addClass("activeLst");
        });
        function getPageName(url) {
            var index = url.lastIndexOf("/") + 1;
            var filenameWithExtension = url.substr(index);
            var filename = filenameWithExtension.split(".")[0];
            return filename;                                    
        }
        function redirectOnClick(item) {
            debugger;
            window.location.href = $(item).text() + ".aspx";
        }
    </script>

StyleSheet:

.menuLst
{
    float:left;
    display:inline;
    background-color:Gray;
    padding:2x;
}
.menuLst a
{
   color:Black;
   text-decoration:none;
}
.menuLst a:hover
{
   color:white;
   text-decoration:none;
}

.menuLst a:active
{
   color:Green;
   text-decoration:none;
}
.menuLst:hover
{
   background-color:Black;
   text-decoration:none;
}
.active
{
    color:Green !important;
   text-decoration:none;
}
.activeLst
{
    background-color:Black;
}

Condition: PageName and ID used for Menu Item must be same

Lakshman
  • 65
  • 9
  • It highlights the menu item on hover and not the current active item:( –  Jan 08 '14 at 10:24
  • it will also highlight the current item. On click the page redirection happens and on doc.ready i am checking the pagename and applying styles for the menuitem. did you make sure that the ItemID, Page names are matching? Ex : Item ID - MenuItem1, PageName = MenuItem1.aspx or PageName = MenuItem1.html – Lakshman Jan 08 '14 at 10:35
  • Yes they are same. Is it not possible to make it happen in the css I posted. –  Jan 08 '14 at 10:45
  • I think the problem is there in the below mentioned lines: $("#" + pageName).addClass("active"); $("#" + pageName).parent().addClass("activeLst"); Can you check whether you are able to get the menu item using the above lines while debugging? I will check the CSS you have posted. – Lakshman Jan 08 '14 at 11:07
0

Working with Bootstrap and jQuery

$(function () {
        $(".navbar‐btn a").each(function () {                  
            if (this.href.trim().split("/").splice(3, 4)[0] == window.location.pathname.split("/").splice(1, 1)[0])
                $(this).parent().addClass("active");
        });
    })
Dionny Prensa
  • 139
  • 3
  • 5