ASP.NET how to disable button on submit?
I have forms with payment in ASP.NET site, when user clicks on asp:B开发者_如何学Pythonutton sometime he submits few times. I want to disable this functionality without harming current ASP.NET site ( by that i mean no fancy jQuery, unless it is very basic and will not interfere with ASP.NET built-in validation)
thanks!!!
You can easily do this on the button in 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 is still not working, you can initiate the postback yourself before disabling the button:
onClientClick="if(Page_ClientValidate()){__doPostBack;this.disabled=true;}"
onClientClick="if(Page_ClientValidate()){__doPostBack;this.disabled=true;}"
UseSubmitBehavior="false"
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() + "}");
}
Create a form and put a button in your form (Remember to set up the validationGroup property if there is more than one form/submit):
<asp:Button runat="server" ID="btnSubmit" OnClick="btnSubmit_Click" />
Call it in page load:
CommonFunctions.disableSubmitButton(btnSubmit);
I know this is an old question but all the answers fail in some circumstances. Mainly you risk getting a "Page_ClientValidate is not defined" error in the browser.
This approach does work:
public static void DisableSubmitButton(Button button)
{
button.Attributes.Add("onclick", "this.disabled=true;" + button.Page.ClientScript.GetPostBackEventReference(button, String.Empty));
}
you might go for blockUI. it's really nice, as it blocks the complete ui, not only the button - so that a user can't click on another button (eg. you want to save ... takes some time ... user clicks on delete meanwhile)
精彩评论