Mandragorasprout
Mandragorasprout

Reputation: 123

How to add "active" css class to linkbutton code behind

I have several linkbuttons in my master page. I need to add css class "Active" after I click each linkbutton and postback URLs.

<asp:LinkButton ID="Linkbutton1" runat="server" PostBackUrl="/News.aspx?lang=1"
         Text="News" OnClick="Linkbutton1_Click">
</asp:LinkButton>

Linkbutton 1

Linkbutton 2 - class "active"

Linkbutton 3

I tried to add class using linkbutton onclick event, but after postback css class has been removed.

Upvotes: 2

Views: 9481

Answers (3)

navya
navya

Reputation: 144

onclick of linkbutton call javascript function changestyle(SenderID)

<script type="javascript">
var strPreviousCahnge=""
function changestyle(SenderID)
{
   var LinkButtonActive=document.getelementbyid(senderID);
LinkButtonActive.className="subTabActive";

            if (strPreviousCahnge!= "" && strPreviousCahnge!= id)
            {
                var identity=document.getElementById(strPreviousCahnge);
                identity.className="subTabInactive";
            }
strPreviousCahnge=SenderID
}
</script>

Upvotes: 0

mehdi
mehdi

Reputation: 1755

if you want write in code behind you can work with cookies:

in Linkbutton1_Click method:

Response.Cookies["Linkbutton1-cssClass"].Value = "active";

in Page_Load method:

if(Request.Cookies["Linkbutton1-cssClass"] != null)
   Linkbutton1.CssClass = Server.HtmlEncode(Request.Cookies["Linkbutton1-cssClass"].Value);

finaly you can use foreach for all LinkButtons

Upvotes: 1

Artem Koshelev
Artem Koshelev

Reputation: 10604

Put this to the Linkbutton1_Click method:

Linkbutton1.CssClass = "active";

Article on MSDN.

Upvotes: 2

Related Questions