How to set active class to the current page link if your navigation menu is in a master page ?
Assuming that your menu structure is as shown below in the (Master.aspx) file :
<ul>
<li><a href="Dash.aspx">Dashboard</a></li>
<li><a href="Tests.aspx">Test</a></li>
<li><a href="Report.aspx">Report</a></li>
</ul>
Start by assigning a unique (id) to each element and add the key (runat="server") to each. so your markup would look like this
<ul>
<li id="dashlink" runat="server"><a href="Dash.aspx">Dashboard</a></li>
<li id="testslink" runat="server"><a href="Tests.aspx">Test</a></li>
<li id="reportlink" runat="server"><a href="Report.aspx">Report</a></li>
</ul>
Finally in the code-behinde of the Master Page, add the following to the (Page_load) function
protected void Page_Load(object sender, EventArgs e)
{
String activepage = Request.RawUrl;
if (activepage.Contains("Dash.aspx"))
{
dashlink.Attributes.Add("class", "active");
}
else if (activepage.Contains("Tests.aspx"))
{
testslink.Attributes.Add("class", "active");
}
else if (activepage.Contains("Report.aspx"))
{
reportlink.Attributes.Add("class", "active");
}
}
Enjoy!