ASP.NET how to disable a button on submit?

I have payment forms on an ASP.NET site, when a user clicks on asp: Button, he once submits several times. I want to disable this functionality without harming the existing ASP.NET site (by this I mean not fantastic jQuery, if it is not very simple and will not interfere with ASP.NET built-in validation)

thank!!!

+5
source share
5 answers

You can easily do this on a button on the client side:

onClientClick="this.disabled=true;"

You can also use the approach from Muhammad with this:

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

If this still does not work, you can initiate a callback before disabling the button:

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

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 object</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() + "}");
}

( validationGroup, form/submit):

<asp:Button runat="server" ID="btnSubmit" OnClick="btnSubmit_Click" />

:

CommonFunctions.disableSubmitButton(btnSubmit);
+2
onClientClick="if(Page_ClientValidate()){__doPostBack;this.disabled=true;}" 
UseSubmitBehavior="false" 
+2

, , . "Page_ClientValidate is not defined" .

:

public static void DisableSubmitButton(Button button)
{
    button.Attributes.Add("onclick", "this.disabled=true;" + button.Page.ClientScript.GetPostBackEventReference(button, String.Empty));
}
+2

blockUI. , ui, - (, ... ... )

+1

All Articles