Disable button on postback when using asp.net validation controls
I'm using the following code to disable my button when it is clicked:
OnClientClick="this.disabled = true" UseSubmitBehavior="false"
开发者_高级运维The problem is that the page has several asp.net validation controls. So when I click the button and the button disables. If any of the validation controls has failed, the button stays disabled (and never gets enabled - because a postback is never executed).
I doubt there is a .net way to solve this, so I hope there is some kind of javascript that can be inserted in the OnClientClick-property. Hopefully I can check if the validation controls have returned any error, before i disable the button...
Trigger the validation on your own, and use the validation to determine whether the button should stay disabled.
Your button (note that OnClientClick is returning true/false):
<asp:Button ID="Button1" runat="server" Text="Go" OnClientClick="return validatePage(this);" ... />
Your JavaScript function:
validatePage = function(button)
{
var isValid = Page_ClientValidate();
button.disabled = isValid;
//return validation - if true then postback
return isValid;
}
This question has been asked before. Here are just a few answers I found that may help you:
JQuery validation plugin re-enable submit after validation
Re-Enable button after submit
Use jQuery to Enable/Disable a Button based on vars
The onsubmit event on form1 shos how to do it in a simple way. Try this:
<form id="form1" runat="server" onsubmit="setTimeout(function() {form1.btn1.value = 'Wait...'; form1.btn1.disabled = true; }, 0);">
<div>
<asp:Label id="lbl1" runat="server" Text="Simple test:" AssociatedControlID="text1" />
<asp:TextBox runat="server" ID="text1" />
<asp:RequiredFieldValidator ErrorMessage="Fill the TextBox!" ControlToValidate="text1" runat="server" />
<asp:Button Text="text" runat="server" ID="btn1" OnClick="btn1_Click" />
</div>
</form>
精彩评论