Highlighting a menu bar in an ASP.NET web application

I created a web application using ASP.NET Visual Studio 2010 with the main pages. As you will see, the project gives us the default menu item. I have 5 pages (links) listed in these menus. Now, when the user goes to a specific page, I want to highlight this link in the menu bar. I do not know how to do that: (

I tried this in the code of the main page, but it did not work either:

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

And in my markup view, I have the following:

 <div class="clear hideSkiplink">
            <asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal"  OnMenuItemClick="NavigationMenu_MenuItemClick">
                <Items>
                    <asp:MenuItem Text="Test1"/>
                     <asp:MenuItem Text="Test2"/>
                     <asp:MenuItem  Text="Test3"/>

                </Items>
            </asp:Menu>
        </div>

Therefore, when the user comes to the Test1.aspx page, I want to highlight the menu item Test1. How should I do it?

Any help would be appreciated! Thank...

+3
source share
2 answers

Use CSS link classes to implement this:

http://www.w3schools.com/css/sel_active.asp

"" - , HTML. :

<ul class="menu">
    <li>
        <a href="~/Home" id="link1" title="First Link" runat="server">Link 1</a>
    </li>
</ul>

, , - , ( ) .

0

, Id

, Load loader, -

if (!Page.IsPostBack)
{
      if(Page is Default)
           liHome.Attributes["class"] += " active";

}

"" . css

.active
{
    background-color: Red;
}
0

All Articles