Apply active CSS on Menus on Master Pages

Introduction

Apply active CSS on menus on Master Pages from code behind without JavaSrcipt.

In our web applications

We face problems when using a Master Page. How to set the class for the active tab?

Here we can set active class

  1. <ul>  
  2.      <li id="page1" runat="server" ><a href="studentHomePage.aspx"> Dashboard    </a></li>  
  3.       
  4.       <li id="page2"  runat="server"><a href="Mytest.aspx">   My Test</a></li>  
  5.   
  6.        <li id="page3"  runat="server"><a href="TestReport.aspx">Test Report</a></li>  
  7.   
  8.         <li id="page4"  runat="server"><a href="Subscription_New.aspx">Subscribe</a></li>   
  9.                  
  10.          
  11.  </ul>  
Code Bheind
  1. page_load  
  2. {  
  3.         String activepage= Request.RawUrl;  
  4.        if(activepage .Contains("studentHomePage.aspx"))  
  5.        {  
  6.             page1.Attributes("class") = "active";  
  7.        }  
  8.        else if(activepageactivepage .Contains("Mytest.aspx"))  
  9.        {  
  10.             page2.Attributes("class") = "active";  
  11.        }  
  12.           else if( activepage.Contains("TestReport.aspx") )  
  13.        {  
  14.             page3.Attributes("class") = "active";  
  15.           }  
  16.           else if( activepage.Contains("Subscription_New.aspx"))  
  17.        {  
  18.             page4.Attributes("class") = "active";  
  19.           }  
  20.   
  21. }  

Screen Shots

Tab 1

screen shots

Tab 2

output
Ebook Download
View all
Learn
View all