javascript 如何在客户端禁用 asp.net 验证器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11672388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:57:55  来源:igfitidea点击:

How to disable asp.net validator on client side?

javascriptasp.netvalidation

提问by ihorko

I have very simple aspx page:

我有非常简单的 aspx 页面:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>

<body>
    <form id="form1" runat="server">
    <div>
        <asp:textbox runat="server" ID="tbText" ValidationGroup="Address"></asp:textbox>
        <br />
        <asp:RequiredFieldValidator ID="rfvText" runat="server" ControlToValidate="tbText" 
            ValidationGroup="Address" ErrorMessage="RequiredFieldValidator">Enter text</asp:RequiredFieldValidator>
        <br />
        <asp:Button runat="server" Text="Submit" ID="btnSubmit" OnClick="Submit_Click" 
            ValidationGroup="Address" OnClientClick="DisableValidator();" />
        <script type="text/javascript">
            function DisableValidator() {
                alert('Called and disable validators before submit');

                var validator = document.getElementById("<%=rfvText.ClientID%>");
                validator.validationGroup = "someGroup";
                ValidatorEnable(validator, false);
            }
        </script>
    </div>
    </form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Submit_Click(object sender, EventArgs e)
    {
        Validate("Address");
        if (!IsValid)
        {
            throw new Exception("Page is no valid");
        }
    }
}

All elements on the page have ValidationGroup="Address", but I need disable my validator just before I click on the button. So, on client site it disables, but when I try to validate it on the server, my page isn't valid on the server but valid on client.

页面上的所有元素都有 ValidationGroup="Address",但我需要在单击按钮之前禁用我的验证器。因此,在客户端站点上它禁用,但是当我尝试在服务器上验证它时,我的页面在服务器上无效但在客户端上有效。

How can I disable validator on client to became disable on the server also?

如何禁用客户端上的验证器以在服务器上也禁用?

Thanks!

谢谢!

采纳答案by Brian Mains

You would have to programmably disable it when the page posts back, however you interpret that validator should be disabled on the client then would need to be replicated on the server, and set Enabled="false".

当页面回发时,您必须以可编程方式禁用它,但是您解释应该在客户端上禁用验证器,然后需要在服务器上复制,并设置Enabled="false".

回答by acarlos

You should use EnableClientScript="False"on the validation control, that is:

您应该EnableClientScript="False"验证控件上使用,即:

<asp:RequiredFieldValidator ID="rfvText" 
      runat="server" ControlToValidate="tbText"
      ValidationGroup="Address" 
      EnableClientScript="False"
      ErrorMessage="RequiredFieldValidator">Enter Text
 </asp:RequiredFieldValidator>

回答by Jeff

If you are using .NET 4, try using the EnableClientScriptproperty. I've never used it myself but according to the docs, it should meet your needs.

如果您使用的是 .NET 4,请尝试使用该EnableClientScript属性。我自己从未使用过它,但根据文档,它应该可以满足您的需求。

Gets or sets a value indicating whether client-side validation is enabled.

获取或设置一个值,该值指示是否启用客户端验证。

<asp:Button runat="server" Text="Click Me" EnableClientScript="False" />