Disable asp.net button after click to prevent double clicking

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?


Solution 1:

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;

Solution 2:

I have found this, and it works:

    "this.disabled = true;" + ClientScript.GetPostBackEventReference(btnSave, null) + ";");

Solution 3:

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


If you have e.g.

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

Then you can use

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

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

Solution 4:

If you want to prevent double clicking due to a slow responding server side code then this works fine:

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

Try putting a Threading.Thread.Sleep(5000) on the _Click() event on the server and you will see the button is disabled for the time that the server is processing the click event.

No need for server side code to re-enable the button either!

Solution 5:

You can use the client-side onclick event to do that:

yourButton.Attributes.Add("onclick", "this.disabled=true;");

Solution 6:

<asp:Button ID="btnSend" runat="server" Text="Submit"  OnClick="Button_Click"/>

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