I have an old ASP.NET web forms application (.NET 2.0) that has a process that takes 30 seconds or so to run when a button is clicked. I need to prevent the button from being clicked twice. This will prevent posting twice, which causes duplicate data in a database.
I tried adding;
OnClientClick="this.disabled='true';"
to try to disable the button in JavaScript. However, though the button becomes disabled, the postback for the button then doesn't work.
Disabling the button to prevent multiple submissions is the ideal for this application. How can I do this?
EDIT
I am unable to use third-party controls.
To circumvent all validation and validation group issues, I found it easiest to use the
window.onbeforeunload
event, like soThe above script disables the ASP.Net Button as soon as the page is ready to do a PostBack including validation or the ASP.Net form is submitted.
Disable ASP.Net button after click to prevent double clicking
One thing you can do is after clicking it hide it in the client side and show some loader images.
Or if its a form or something you can add recaptcha
You must return true after the OnClientClick:
Any clicks in client-side must return true if the postback is to continue, it's a means of providing client-side validatin on button presses.
After searching for a while, I came up with this solution.
The problem with the UseDefaultBehavior solution is that the Enter key stops not submitting the form which is used a lot. I am assuming that a considerable percentage of users try enter instead of clicking.
From an aspx.cs file you can add the script to address this (this is the only solution worked for me and was provided by: wexxix)
Obviously you can just add the script to the page, I just used like that since I just had access to the customer C# code. Thanks
If you simply disable the button then ASP.NET won't post the form. Also you want to deal with client-side validation. Here's my solution:
See @Dave Ward's post for a description of UseSubmitBehavior.
If you have multiple validation groups you'll need to enhance this solution for that.