Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable asp.net button after click to prevent double clicking

I have an ASP.NET button that I need to disable after the user clicks it to prevent double-clicking. Once the submit completes it has to be enabled again. Can anyone help me with this?

like image 611
Goutham Avatar asked Mar 04 '11 04:03

Goutham


People also ask

How can we prevent double clicking or multiple form submission after ASP NET button is clicked?

On Click of ASP.Net Submit button, we disable the button using javascript so that user cannot submit it again. But disabling the button will prevent the form submission, as you cannot submit the form if the button is disabled. So we call the ASP.Net PostBack event method directly.

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 can prevent double click on submit button in asp net MVC?

I have the following code for avoiding the double clic: jQuery. fn. preventDoubleSubmit = function () { var alreadySubmitted = false; return jQuery(this).

How can stop postback in button click in asp net?

The postback on submit button can be avoided by giving return=false in the event handler function as below.


3 Answers

Here is a solution that works for the asp.net button object. On the front end, add these attributes to your asp:Button definition:

<asp:Button ... OnClientClick="this.disabled=true;" UseSubmitBehavior="false" /> 

In the back end, in the click event handler method call, add this code to the end (preferably in a finally block)

myButton.Enabled = true; 
like image 145
kmonty Avatar answered Sep 30 '22 09:09

kmonty


I have found this, and it works:

btnSave.Attributes.Add(
    "onclick", 
    "this.disabled = true;" + ClientScript.GetPostBackEventReference(btnSave, null) + ";");
like image 24
gkneo Avatar answered Sep 30 '22 10:09

gkneo


Check this link, the most simplest way and it does not disable the validations.

http://aspsnippets.com/Articles/Disable-Button-before-Page-PostBack-in-ASP.Net.aspx

If you have e.g.

  <form id="form1" runat="server">
      <asp:Button ID="Button1" runat="server" Text="Button1" OnClick="Button1_Clicked" />
  </form>

Then you can use

  <script type = "text/javascript">
  function DisableButton() {
      document.getElementById("<%=Button1.ClientID %>").disabled = true;
  }
  window.onbeforeunload = DisableButton;
  </script>

To disable the button if and after validation has succeeded, just as the page is doing a server postback.

like image 31
mohan padmanabhan Avatar answered Sep 30 '22 10:09

mohan padmanabhan