Set MenuItem as Active using CSS class based on URL of the Current Page in ASP.Net

Last Reply on Aug 18, 2012 04:05 AM By Mudassar

Posted on Aug 16, 2012 01:58 AM


    I have a .aspx page where i copied 1 navigation bar. On it i put some link buttons. They are navigating. But when I click it's class should become Active <li class="active">Home</li>. If i click on register link button then register's class should become active. 



Results 1 - 5 of 11 123
Posted on Aug 16, 2012 02:02 AM

You can check the URL and based on that select the Menu link

<li class='<%=Request.Url.AbsolutePath.ToLower().Contains("home") ? "active" : "" %>'>Home</li>
<li class='<%=Request.Url.AbsolutePath.ToLower().Contains("contact") ? "active" : "" %>'>Contact</li>


Posted on Aug 16, 2012 09:10 AM

i don't understand where to write it?


Posted on Aug 16, 2012 09:14 AM

In the aspx page where you have <li>

Posted on Aug 16, 2012 10:03 AM

<li class='<%=Request.Url.AbsolutePath.ToLower().Contains("Home") ? "active" : "" %>'><asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Home</asp:LinkButton></li>

      <li class='<%=Request.Url.AbsolutePath.ToLower().Contains("Contact") ? "active" : "" %>'><li><asp:LinkButton ID="LinkButton2" runat="server" onclick="LinkButton2_Click">Contact</asp:LinkButton></li>  


I wrote above code. but it is not working

Posted on Aug 16, 2012 10:19 AM

For this to work your page must contain words Home and Contact. Put your page names instead