Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent users clicking a button twice?

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.

like image 376
Richard Avatar asked May 26 '11 10:05

Richard


People also ask

How do I stop my keys from double clicking?

Present Code click(function (e) { // Prevent button from double click var isPageValid = Page_ClientValidate(); if (isPageValid) { if (isOperationInProgress == noIndicator) { isOperationInProgress = yesIndicator; } else { e.

How can we prevent user from clicking button multiple times in asp net?

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

How do I stop a button from clicking?

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.

How do I turn off multiple click on Android?

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.


Video Answer


1 Answers

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.

like image 60
Rory Avatar answered Sep 22 '22 23:09

Rory