Question

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.

Was it helpful?

Solution

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/

OTHER TIPS

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.

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

<script type="text/javascript">
    // 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);
        }
    }
</script>

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

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.

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

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;"/>

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top