How to Disable ASP.NET Submit button after first Submit

By Peter Bromberg

If you have a form (for payment, etc) and you want to prevent multiple form submissions there are several techniques you can use.

First, you can do this client-side:

onClientClick="this.disabled=true;"

You can also use this approach when validation is used:

onClientClick="if(Page_ClientValidate()){this.disabled=true;}"

If this is still not working, you can initiate the postback yourself before disabling the button:

onClientClick="if(Page_ClientValidate()){__doPostBack;this.disabled=true;}"

Another technique:

Create a static method:

/// <summary>
/// Disable the button on submit. Remember to set up the validationGroup property if there is more than one form/submit
/// </summary>
/// <param name="objButton">The submit button </param>
public static void disableSubmitButton(Button objButton)
{
    objButton.CausesValidation = false;
    string validationGroup = objButton.ValidationGroup;
    if (string.IsNullOrEmpty(validationGroup))
        objButton.Attributes.Add("onclick", "if (Page_ClientValidate()) {this.value=\"Processing...\";this.disabled=true;" + objButton.Page.ClientScript.GetPostBackEventReference(objButton, "").ToString() + "}");
    else
        objButton.Attributes.Add("onclick", "if (Page_ClientValidate(\"" + validationGroup + "\")) {this.value=\"Processing...\";this.disabled=true;" + objButton.Page.ClientScript.GetPostBackEventReference(objButton, "").ToString() + "}");
}

Create a form -  put a button on your form (Remember to set up the validationGroup property if there is more than one form/submit):

Call it in Page_Load:

CommonFunctions.disableSubmitButton(btnSubmit);


And another suggestion:

onClientClick="if(Page_ClientValidate()){__doPostBack;this.disabled=true;}"
UseSubmitBehavior
="false"

How to Disable ASP.NET Submit button after first Submit  (3056 Views)