简体   繁体   English

使用asp.net验证控件时禁用回发按钮

[英]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. 问题在于该页面具有多个asp.net验证控件。 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. 我怀疑是否可以通过.net方式解决此问题,所以我希望可以在OnClientClick属性中插入某种javascript。 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): 您的按钮(请注意,OnClientClick返回true / false):

<asp:Button ID="Button1" runat="server" Text="Go" OnClientClick="return validatePage(this);" ... />

Your JavaScript function: 您的JavaScript函数:

validatePage = function(button)
{
    var isValid = Page_ClientValidate();
    button.disabled = isValid;

    //return validation - if true then postback
    return isValid;
}

The onsubmit event on form1 shos how to do it in a simple way. 在form1上的onsubmit事件显示了如何以一种简单的方式进行操作。 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>

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 jQuery验证插件在验证后重新启用提交

Re-Enable button after submit 提交后重新启用按钮

Use jQuery to Enable/Disable a Button based on vars 使用jQuery启用/禁用基于vars的按钮

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM