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.
You need to also set the Button's
UseSubmitBehavior
property tofalse
.Web browsers do not include disabled elements in a form's POST data, even if the element happens to be the button that triggered the form submission in the first place. Unfortunately, ASP.NET WebForms' single-form-page design relies on that bit of information to know which control raised the PostBack and which event handler to execute (i.e. Button1.On_Click).
Switching to
UseSubmitBehavior="false"
injects a__doPostBack('Button1', '')
in the client-side onclick handler to work around that problem. Then, even if you disable the Button, the__doPostBack
parameter lets ASP.NET know which control raised the PostBack and which events to fire on the server-side.Advantages
Limitations
† Most other solutions I have seen have these same limitations
Code
Simply place this snippet at the bottom of your HTML code before the closing
</body>
tag.Most of this solutions provided above that suggest disabling won't work because you won't get a PostBack. This is the simplest and cleanest working solution I've seen:
In the Page_Load event of your form put this line:
Source: dotnetodyssey.com
I found that link very useful to solve this problem Disable ASP.Net button after click to prevent double clicking
I hope that can help :)
I usually add this method to my BasePage for reuse.
This code handle validations too