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.

4 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 ivan on 10.12.07 at 4:12 am

Can you show all the code for the aspx page?
Thanks

Leave a Comment