Disable Button after ASP.NET Async Webservice Call

With ASP.NET Ajax and ASP.NET v2.0 it is a piece of cake to create an asynchronous Webservice these days.

However, what if that needs about 45 minutes or even longer to complete? You know your users, many people will hit the button again after 10 minutes because they think it will be faster that way, or they accidently double click it. Just imagine people standing at a traffic light and hitting the “Get Green Button” like a thousand times. The result of this is an unwanted increase of the server load by starting an additional webservice process with every click.

Here is a very easy solution to pretend this, add this to the Button which fires the async webservice call, in my case Button1:

   41 Button1.Attributes.Add(“onclick”, ” this.disabled = true; “ + ClientScript.GetPostBackEventReference(Button1, null) + “;”);

That’s it! The button will be disabled until the final PostBack is taking place. Of course your controls need to be in an UpdatePanel for this to work.

7 comments ↓

#1 Steven Harman on 07.07.07 at 5:25 am

You could also use the PageRequestManager’s isInAsyncPostBack property to determine if an Async event is currently taking place (in your UpdatePanel). Then you could ignore the click or even show a message letting the user know that something is happening and clicking the button won’t make it happen any faster. :)

You can read more on the PageRequestManager here.

#2 andreas.kraus on 07.07.07 at 10:31 am

Good alternative way Steven, thanks for the input!

#3 Ben on 08.28.07 at 5:58 pm

:smile:
Thank you so much for posting this. I have been searching for two days on how to do this, but not using a webservice.

Thank you very much!

#4 D on 12.29.08 at 12:49 am

Thanks Andreas! This was EXACTLY what I was looking for, and really saved me quite a few headaches.

#5 Jimi J on 01.16.09 at 7:43 pm

This works fine if you you have no validitors on the page. I had issues with validators using this code.

#6 ramesh on 05.06.09 at 7:55 am

thank you. its working good, yes still i have done this concept with another one hidden button visible true and false through javascript.
This post is make my work very simple, thanks again

#7 Ashok on 01.19.10 at 6:26 am

Thanks Andreas Kraus…That worked like a gem. Good Work!!!

Leave a Comment