Preventing accidental double clicking on a button

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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/

Method 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.

Method 3

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.

Method 4

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.

Method 5

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

Method 6

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

Method 7

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x