Change CssClass on Attribute.Add()

kapes13
kapes13 used Ask the Experts™
on
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?

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
CssClass is a server-side term. You would want to use className on the client-side:
Button1.Attributes.Add("onclick", "this.disabled = true;" + ClientScript.GetPostBackEventReference(Button1, null) + ";return false;this.className = FormButtonFiltered;");

Open in new window

Author

Commented:
Thanks, but that does not work either.  Maybe because I am using return false for client side behavior?
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
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?
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
P.S.

Put quotes around the class name:

this.className = 'FormButtonFiltered';

Open in new window

Author

Commented:
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...............
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
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.
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
You could always check the IsPostBack button
I meant "property", not "button".

Author

Commented:
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.

Author

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial