最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to disable asp.net validator on client side? - Stack Overflow

programmeradmin0浏览0评论

I have very simple aspx page:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ".dtd">

<html xmlns="">
<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.

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

Thanks!

I have very simple aspx page:

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

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

<html xmlns="http://www.w3/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.

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

Thanks!

Share Improve this question asked Jul 26, 2012 at 15:08 ihorkoihorko 6,95526 gold badges80 silver badges123 bronze badges 1
  • Below solution works fine for me. My response from other post – Sedat Kumcu Commented Jun 14, 2021 at 10:45
Add a ment  | 

3 Answers 3

Reset to default 2

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

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

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".

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

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

<asp:Button runat="server" Text="Click Me" EnableClientScript="False" /> 
发布评论

评论列表(0)

  1. 暂无评论