Link to home
Get AccessLog in
Avatar of kapes13

asked on

Change CssClass on Attribute.Add()

So I have this code to disable an ASP.NET button server side and that part works but I also need to set CssClass on the same button to change the style onclick:

I am using:
            Button1.Attributes.Add("onclick", "this.disabled = true;" + ClientScript.GetPostBackEventReference(Button1, null) + ";return false;this.CssClass = FormButtonFiltered;");

Which is not changing the CssClass.  What code am I missing?

Avatar of kaufmed
Flag of United States of America image

Link to home
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
Avatar of kapes13


Thanks, but that does not work either.  Maybe because I am using return false for client side behavior?
You've got two issues:

As you stumbled upon, you should move the class changing functionality before the "return false;"
You're doing a PostBack...  when the server sends out the request, its basically resetting the page to a state similar to what it had before the PostBack. That is, your button is enabled because it is enabled in your markup.

What is your ultimate goal?

Put quotes around the class name:

this.className = 'FormButtonFiltered';

Open in new window

Avatar of kapes13


You are right, thanks for the explanation.

I am just doing a disable on the click of the ASP button so the post back does not happen twice, there are not a lot of good quick examples of this online so I grabbed something and can't get all the functionality in place, works great alone as a button, but of course now it requires some neat styling to make the button look cool....................which .NET button does not have, but you can set the css class and style it that way.

Much obliged.  May not make it back to this until later...............
You could always check the IsPostBack button and if true, then set the Button's Enabled property to false. Then on the subsequent page generation on the client, the Button will be disabled.
You could always check the IsPostBack button
I meant "property", not "button".
Avatar of kapes13


Thanks - I have already done your suggestions and although the IsPostBack check works it is too late in the event chain to set the style and the client will never see the styles change.
Avatar of kapes13


Thank you - I got this sorted out and even display a prompt while the operation is progressing.  Thanks for sticking with me and take care.