Click here to Skip to main content
14,921,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to show active my menu item when i am click on it but the menu is on my masterpage that why i am not able to do this task. can any one guide me??

<div class="menu_nav">
          <li ><a href="Home.aspx" class="active">Home</a></li>
          <li ><a href="Services.aspx" class="inactive">Services</a></li>
          <li><a href="Aboutus.aspx" class="inactive">About Us</a></li>
          <li><a href="Login.aspx" class="inactive">Login</a></li>
          <li><a href="Contactus.aspx" class="inactive">Contact Us</a></li>

my code in masterpage like this and I want to show active menu item when i am click on it for example when I click on home its highlight home and soon what will I do for it??

moved from answer[/EDIT]
Updated 23-Mar-11 2:12am
Sandeep Mewara 23-Mar-11 6:32am
Did you try to Google or even CP Search?

You should set value for StaticSelectedStyle-CssClass attribute. It will look like:

<asp:Menu ID="Menu1" runat="server" StaticSelectedStyle-CssClass="yourcssclass">
                <asp:MenuItem Text="Home"></asp:MenuItem>
                <asp:MenuItem Text="About"></asp:MenuItem>

Please vote if my answer will help you.
m@dhu 23-Mar-11 8:13am
op updated his question. Have a look at it.
You can do following thing in your Content page :-
Just find control from Master Page ,

HtmlAnchor example = (HtmlAnchor)Master.FindControl("example");
example.class = "selected";

For HtmlAnchor you have to add "System.Web.UI.HtmlControls" namespace.

But it will gives u error in " Example.Class " bcz it won't get class attributes.

See here is the w3 specification for the anchor (a) tag:

1. <!ELEMENT A - - (%inline;)* -(A) -- anchor -->
3. %attrs; -- %coreattrs, %i18n, %events --
4. charset %Charset; #IMPLIED -- char encoding of linked resource --
5. type %ContentType; #IMPLIED -- advisory content type --
6. name CDATA #IMPLIED -- named link end --
7. href %URI; #IMPLIED -- URI for linked resource --
8. hreflang %LanguageCode; #IMPLIED -- language code --
9. rel %LinkTypes; #IMPLIED -- forward link types --
10. rev %LinkTypes; #IMPLIED -- reverse link types --
11. accesskey %Character; #IMPLIED -- accessibility key character --
12. shape %Shape; rect -- for use with client-side image maps --
13. coords %Coords; #IMPLIED -- for use with client-side image maps --
14. tabindex NUMBER #IMPLIED -- position in tabbing order --
15. onfocus %Script; #IMPLIED -- the element got the focus --
16. onblur %Script; #IMPLIED -- the element lost the focus --
17. >

As you can see, there's no class attribute there, so this is just me being a moog.

Annoyingly, it looks like there's no easy way to do what I'm trying to do using the HTML anchor,
so I'll have to use some other element (such as an ASP.NET Button, or something or server Control <asp:HyperLink>). No big deal.

If You use <asp:hyperlink xmlns:asp="#unknown"> then thing will be easy for you,

Master.master Page :-

<asp:hyperlink id="tab_home" runat="server" navigateurl="ABC.aspx">
<asp:hyperlink id="tab_login" runat="server" navigateurl="">

On Content Page : (Here ABC.aspx)

Just Find Ur Control from master page like this

HyperLink hlCart = (HyperLink)Master.FindControl("tab_cart");
hlCart.CssClass = "selected";

Here you can assign CSSCLASS="SELECTED".

I Hope this will Help you.
All d best
In your master page add the following code....

Private mSelMenuItem as string = nothing

Public property SelMenuItem as string
    return mSelMenuItem
  end get
  set (value as string)
    mSelMenuitem = value
  end set
end property

Public Function GetMyMenuState(ForThisMenuItem as string) as string
  If ForThisMenuItem = mSelMenuItem Then
    Return ("class=" & chr(34) & "active" & chr(34) & " ")
    Return ("class=" & chr(34) & "inactive" & chr(34) & " ")
  End If
End Function

then... in EACH of your CONTENT Pages in the form load event add a SINGLE statement to identify the page to the master page...
like this for the homepage...
me.master.SelMenuItem = "Home"

or like this for services page

me.master.SelMenuItem = "Services"

Then in your masterpage where the menu is....
you can use it like this ...

<li><a href="homepage.aspx" <%=GetMyMenuState("Home") %>>Home</a></li>
<li><a href="services.aspx" <%=GetMyMenuState("Services") %>>Services</a></li>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900