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?

.NET Programming

Avatar of undefined
Last Comment

8/22/2022 - Mon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

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

Your help has saved me hundreds of hours of internet surfing.

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".
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.

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.

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.