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?
I am unable to use third-party controls.
Present Code click(function (e) { // Prevent button from double click var isPageValid = Page_ClientValidate(); if (isPageValid) { if (isOperationInProgress == noIndicator) { isOperationInProgress = yesIndicator; } else { e.
submit(function () { $('input[type=submit][data-loading]'). addClass('disabled'); if ($(this). data('submitted') == true) { $('input[type=submit][data-loading]'). attr('disabled', 'disabled'); return false; } $(this).
To disable a submit button, you just need to add a disabled attribute to the submit button. $("#btnSubmit"). attr("disabled", true); To enable a disabled button, set the disabled attribute to false, or remove the disabled attribute.
The actual solution to this problem is to use setEnabled(false) which greys out the button, and setClickable(false) which makes it so the second click can not be received I have tested this and it seem to be very effective.
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:
<asp:Button runat="server" ID="btnSave" Text="Save" OnClick="btnSave_Click" OnClientClick="if (!Page_ClientValidate()){ return false; } this.disabled = true; this.value = 'Saving...';" UseSubmitBehavior="false" />
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With