views:

655

answers:

4

I have a few controls that inherit from ASP.NET buttons and use 'onserverclick'.

If the user clicks twice, the button fires two server side events. How can I prevent this?

I tried setting "this.disabled='true'" after the click (in the 'onclick' attribute) via javascript, but that blocks the first postback as well.

A: 

Someone else said this somewhere on here a few days ago, and I concur - use javascript to simply hide the button instead of disabling it; you could show a "spinner" image in its place, which lets the user know what is going on.

Jason Bunting
I'm not convinced that I like the idea of buttons disappearing. Unless another (disabled button) took their place. It might get confusing to the end user... no?
Rob Rolnick
I agree with Rob, I don't like buttons dissapearing
Juan Manuel
+8  A: 

See this example for disabling control on postback. It should help you do what you're trying to achieve.

http://encosia.com/2007/04/17/disable-a-button-control-during-postback/

Ryan Lanciaux
Thanks... I was missing the manual postback reference needed by that technique
Juan Manuel
Unfortunately, changing UseSubmitBehavior to false means your form will no longer support users without JavaScript.
NightOwl888
+1  A: 

Instead of hiding, what I have done is swapping buttons using javascript. Show another greyed out image on the click of the first button.

Gulzar
A: 

You don't necessarily want to show the button disabled on postback. You want to make sure they don't accidentally submit twice. So disabling or hiding the button as a result of a server-side action is already too late in the game. By this point the 2nd request is already on it's way. You need to either do it with javascript or make sure your server side code won't run twice.

Joel Coehoorn