I have some CSS that controls my asp:LinkButton hyperlinks, and I want to find out how I can set the WebControl’s CssClass property for the current page whenever the MasterPage loads.
These asp:LinkButton hyperlinks on the MasterPage are contained in a list:
<ul id="menu">
<li class="sprite">
<asp:LinkButton ID="linkButton1" runat="server" PostBackUrl="~/linkButton1.aspx">
<span>link 1</span>
</asp:LinkButton>
</li>
<li class="sprite">
<asp:LinkButton ID="linkButton2" runat="server" PostBackUrl="~/linkButton2.aspx">
<span>link 2</span>
</asp:LinkButton>
</li>
<li class="sprite">
<asp:LinkButton ID="linkButton3" runat="server" PostBackUrl="~/linkButton3.aspx">
<span>link 3</span>
</asp:LinkButton>
</li>
</ul>
In the MasterPage I have tried to set the CssClass using the Page_Load event:
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
linkButton1.CssClass = "";
linkButton2.CssClass = "";
linkButton3.CssClass = "";
string linkButtonID = Request.RawUrl;
if (-1 < linkButtonID.IndexOf("linkButton2")) {
linkButton2.CssClass = "active";
} else if (-1 < linkButtonID.IndexOf("linkButton3")) {
linkButton3.CssClass = "active";
} else {
linkButton1.CssClass = "active"; // default
}
}
}
My CssClass="active" does not appear to ever be set, and looking at the page source quickly verifies that none of my LinkButton controls has the CssClass property set.
Since this is hard to visualize, I’ve put together a jsFiddle. The site does not support ASP, but it may help you visualize what I am trying to accomplish:
http://jsfiddle.net/jp2code/kZQwC/
How would I get my Active Page to set the CssClass?
You are assign the class in
!IsPostBackI am afraid that your url at that time is not having linkButton1, linkButton2 or linkButton3.You take the
class assignment code out of the if conditionand check if you that those condition executed and getting the class.Change
To