8

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.

Priyanga
  • 143
  • 1
  • 3
  • 16
juan
  • 80,295
  • 52
  • 162
  • 195

7 Answers7

14

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
  • 5,965
  • 2
  • 37
  • 49
2

In case of an updatepanel and a button inside a FormView-Template I use the following approach:

// Using that prm reference, hook _initializeRequest
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(InitializeRequestBuchung);

// Abfangen von Mehrfachklicks auf Buttons für asynchrone Postbacks im Updatepanel
function InitializeRequestBuchung(sender, args) {
  var arrButtonIds = ["ButtonInsert", "ButtonUpdate"];

  // Get a reference to the PageRequestManager.
  var prm = Sys.WebForms.PageRequestManager.getInstance();
  if (prm.get_isInAsyncPostBack() & jQuery.inArray(args.get_postBackElement().id, arrButtonIds) > -1) {
    args.set_cancel(true);
  }
}

This cancels the following postback if an async postback is currently still active. Works perfectly.

Michael M.
  • 10,486
  • 9
  • 18
  • 34
Andreas Krohn
  • 1,246
  • 9
  • 7
2

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
  • 399,467
  • 113
  • 570
  • 794
1

Set the Button property UseSubmitBehavior to false. Then create an OnClientClick function that disables the button. It would look something like this:

<script type="text/javascript">
   function disableFunctn(button){
       button.disabled = true;
   }
</script>
<asp:Button ID="button1" UseSubmitBehavior="false" OnClientClick="disableFunctn(this);"/>

fastest cheapest way:

<asp:Button ID="button1" UseSubmitBehavior="false" OnClientClick="this.disabled=true;"/>
latuya23
  • 11
  • 2
1

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
  • 58,249
  • 14
  • 102
  • 93
  • 1
    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 Sep 08 '08 at 19:06
  • I agree with Rob, I don't like buttons dissapearing – juan Sep 08 '08 at 19:08
  • +1 for the idea of a spinner - showing the user that something is going on. I'd still leave him the possibility/"control" to submit again, if he absolutely insists to. – giraff Jul 28 '11 at 20:06
1

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 Nazim
  • 51,744
  • 26
  • 128
  • 170
0

You can also try for example btnSave.Enable = false; when the button is hit and before the processing for the button is done in the Click Event routine. If you need it to be reset to allow it to be enabled have a separate button that resets the button for reuse.

Another method is to set the button with verification so that the user is asked if they want to Save, it should pop up both times.

Yet another method would be to flag the first occurrence then set a popup for the second to verify a second or subsequent usage.