Page.Validate 方法

定义

指示该页上包含的所有验证控件验证指派给它们的信息。

重载

Validate()

指示该页上包含的所有验证控件验证指派给它们的信息。

Validate(String)

指示指定验证组中的验证控件验证指派给它们的信息。

Validate()

指示该页上包含的所有验证控件验证指派给它们的信息。

public:
 virtual void Validate();
public virtual void Validate ();
abstract member Validate : unit -> unit
override this.Validate : unit -> unit
Public Overridable Sub Validate ()

示例

以下代码示例在方案中调用 Validate 定义了多个不同验证组的页面上的方法。

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ Page Language="C#" %>

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

<script runat="server">

  protected void Button_Click(object sender, EventArgs e)
  {
    switch (((Button)sender).ID)
    {
      case "Button1":
        if (TextBoxValidator1.IsValid)
        {
          Label1.Text = "TextBox validates.";
        }
        else
        {
          Label1.Text = "";
          Label4.Text = "";
        }
        break;
      case "Button2":
        // Must explicitly cause Validate here because 
        // Button2 has CausesValidation set to false.
        Validate("Group2");
        if (CustomValidator.IsValid)
        {
          Label2.Text = "CheckBox validates.";
        }
        else
        {
          Label2.Text = "";
          Label4.Text = "";
        }
        break;
      default:
      Label1.Text = "";
      Label2.Text = "";
      break;
        
    }
  }

  // Custom validator for check box.
  protected void CustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
  {
    args.IsValid = (CheckBox1.Checked == true);
  }

  protected void Page_Load(object sender, EventArgs e)
  {
    if (IsPostBack && Context.Request.Form["__EVENTTARGET"] == "TextBox2")
    {
      // Handle AutoPostBack TextBox.
      Validate("Group3");
      if (Page.IsValid)
      {
        Label3.Text = "AutoPostBack TextBox validates.";
      }
      else
      {
        Label3.Text = "";
        Label4.Text = "";
      }
    }
    
  }

  protected void Button3_Click(object sender, EventArgs e)
  {
    Validate();
    if (Page.IsValid)
      Label4.Text = "All controls valid.";
    else
    {
      Label1.Text = "";
      Label2.Text = "";
      Label3.Text = "";
      Label4.Text = "";
    }

  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Page Validate</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      TextBox
      <asp:TextBox ID="TextBox1" ValidationGroup="Group1" runat="server"></asp:TextBox>
      <asp:RequiredFieldValidator Display="Static" ID="TextBoxValidator1" ValidationGroup="Group1" runat="server" ControlToValidate="TextBox1" ErrorMessage="(please enter some text)" EnableClientScript="False"></asp:RequiredFieldValidator>
      <br />
      CheckBox
      <asp:CheckBox ID="CheckBox1" ValidationGroup="Group2" runat="server" />
      <asp:CustomValidator ID="CustomValidator" ValidationGroup="Group2" runat="server" Text="(this option required)" OnServerValidate="CustomValidator_ServerValidate" EnableClientScript="False"></asp:CustomValidator>
      <br />
      <asp:Button ID="Button1" ValidationGroup="Group1" CausesValidation="true" runat="server" Text="Validate Group1 Controls" OnClick="Button_Click" />
      <asp:Label ID="Label1" runat="server"></asp:Label>
      <br />
      <asp:Button ID="Button2" ValidationGroup="Group2" CausesValidation="false" runat="server" Text="Validate Group2 Controls" OnClick="Button_Click" />
      <asp:Label ID="Label2" runat="server"></asp:Label>
      <br />
      <br />
      AutoPostBack TextBox
      <asp:TextBox AutoPostBack="true" ID="TextBox2" ValidationGroup="Group3" runat="server" CausesValidation="true"></asp:TextBox>
      <asp:RequiredFieldValidator ID="TextBoxValidator2" ValidationGroup="Group3" runat="server" ControlToValidate="TextBox2" ErrorMessage="(please enter some text)" EnableClientScript="False"></asp:RequiredFieldValidator>
      <asp:Label ID="Label3" runat="server"></asp:Label>
      <br />
      <br />
      <asp:Button ID="Button3" CausesValidation="true" runat="server" Text="Validate All Controls" OnClick="Button3_Click" />
      <asp:Label ID="Label4" runat="server"></asp:Label>
    
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>

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

<script runat="server">

  Protected Sub Button_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Select Case CType(sender, Button).ID
      Case "Button1"
        If TextBoxValidator1.IsValid Then
          Label1.Text = "TextBox validates."
        Else
          Label1.Text = ""
          Label4.Text = ""
        End If
      Case "Button2"
        ' Must explicitly cause Validate here because 
        ' Button2 has CausesValidation set to false.
        Validate("Group2")
        If CustomValidator.IsValid Then
          Label2.Text = "CheckBox validates."
        Else
          Label2.Text = ""
          Label4.Text = ""
        End If
      Case Else
        Label1.Text = ""
        Label2.Text = ""
    End Select

  End Sub
  
  Protected Sub CustomValidator_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
     
    args.IsValid = (CheckBox1.Checked)

  End Sub
  
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

    If (IsPostBack) Then
      
    ' Handle AutoPostBack TextBox.
      Validate("Group3")
      If (Page.IsValid) Then
        Label3.Text = "AutoPostBack TextBox validates."
      Else
        Label3.Text = ""
        Label4.Text = ""
      End If
    End If
    
  End Sub

  Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  
    Validate()
    If (Page.IsValid) Then
      Label4.Text = "All controls valid."
    Else
      Label1.Text = ""
      Label2.Text = ""
      Label3.Text = ""
      Label4.Text = ""
    End If
    
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Page Validate</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      TextBox
      <asp:TextBox ID="TextBox1" ValidationGroup="Group1" runat="server"></asp:TextBox>
      <asp:RequiredFieldValidator Display="Static" ID="TextBoxValidator1" ValidationGroup="Group1" runat="server" ControlToValidate="TextBox1" ErrorMessage="(please enter some text)" EnableClientScript="False"></asp:RequiredFieldValidator>
      <br />
      CheckBox
      <asp:CheckBox ID="CheckBox1" ValidationGroup="Group2" runat="server" />
      <asp:CustomValidator ID="CustomValidator" ValidationGroup="Group2" runat="server" Text="(this option required)" OnServerValidate="CustomValidator_ServerValidate" EnableClientScript="False"></asp:CustomValidator>
      <br />
      <asp:Button ID="Button1" ValidationGroup="Group1" CausesValidation="true" runat="server" Text="Validate Group1 Controls" OnClick="Button_Click" />
      <asp:Label ID="Label1" runat="server"></asp:Label>
      <br />
      <asp:Button ID="Button2" ValidationGroup="Group2" CausesValidation="false" runat="server" Text="Validate Group2 Controls" OnClick="Button_Click" />
      <asp:Label ID="Label2" runat="server"></asp:Label>
      <br />
      <br />
      AutoPostBack TextBox
      <asp:TextBox AutoPostBack="true" ID="TextBox2" ValidationGroup="Group3" runat="server" CausesValidation="true"></asp:TextBox>
      <asp:RequiredFieldValidator ID="TextBoxValidator2" ValidationGroup="Group3" runat="server" ControlToValidate="TextBox2" ErrorMessage="(please enter some text)" EnableClientScript="False"></asp:RequiredFieldValidator>
      <asp:Label ID="Label3" runat="server"></asp:Label>
      <br />
      <br />
      <asp:Button ID="Button3" CausesValidation="true" runat="server" Text="Validate All Controls" OnClick="Button3_Click" />
      <asp:Label ID="Label4" runat="server"></asp:Label>
    
    </div>
    </form>
</body>
</html>

注解

当用户单击已 CausesValidation 设置为 true属性的任何 ASP.NET 服务器控件(默认值)时,将调用此方法。 Button其中包括、LinkButtonImageButton和 Web 服务器控件、HtmlInputButtonHtmlButtonHtmlInputImage以及 HTML 服务器控件以及可自动帖子返回到服务器(如TextBoxCheckBoxListControl和控件)的BulletedList控件。

若要禁用页面上任何按钮控件的验证,请将按钮控件 CausesValidation 的属性设置为 false

调用此方法时,循环访问与Page.Validators属性关联的对象中包含的ValidatorCollection验证控件,并调用当前验证组中每个验证控件的验证逻辑。 验证组由将页面发布到服务器的控件确定。 如果未指定验证组,则不使用验证组。

备注

页面验证的行为已更改。 在 ASP.NET 2.0 中,控件不再调用 Page.Validate() 该方法;它们改用该方法 Page.Validate(String) 。 如果在 ASP.NET 2.0 页上使用 Page.Validate() 该方法,将忽略验证组并验证所有控件。

继承者说明

ASP.NET 2.0 不使用该方法 Validate() 。 使用 ASP.NET 2.0 时,重写 Validate(String) 方法以更改页面验证行为。

另请参阅

适用于

Validate(String)

指示指定验证组中的验证控件验证指派给它们的信息。

public:
 virtual void Validate(System::String ^ validationGroup);
public virtual void Validate (string validationGroup);
abstract member Validate : string -> unit
override this.Validate : string -> unit
Public Overridable Sub Validate (validationGroup As String)

参数

validationGroup
String

要验证的控件的验证组名。

示例

以下代码示例在方案中调用 Validate 定义了多个不同验证组的页面上的方法。

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ Page Language="C#" %>

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

<script runat="server">

  protected void Button_Click(object sender, EventArgs e)
  {
    switch (((Button)sender).ID)
    {
      case "Button1":
        if (TextBoxValidator1.IsValid)
        {
          Label1.Text = "TextBox validates.";
        }
        else
        {
          Label1.Text = "";
          Label4.Text = "";
        }
        break;
      case "Button2":
        // Must explicitly cause Validate here because 
        // Button2 has CausesValidation set to false.
        Validate("Group2");
        if (CustomValidator.IsValid)
        {
          Label2.Text = "CheckBox validates.";
        }
        else
        {
          Label2.Text = "";
          Label4.Text = "";
        }
        break;
      default:
      Label1.Text = "";
      Label2.Text = "";
      break;
        
    }
  }

  // Custom validator for check box.
  protected void CustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
  {
    args.IsValid = (CheckBox1.Checked == true);
  }

  protected void Page_Load(object sender, EventArgs e)
  {
    if (IsPostBack && Context.Request.Form["__EVENTTARGET"] == "TextBox2")
    {
      // Handle AutoPostBack TextBox.
      Validate("Group3");
      if (Page.IsValid)
      {
        Label3.Text = "AutoPostBack TextBox validates.";
      }
      else
      {
        Label3.Text = "";
        Label4.Text = "";
      }
    }
    
  }

  protected void Button3_Click(object sender, EventArgs e)
  {
    Validate();
    if (Page.IsValid)
      Label4.Text = "All controls valid.";
    else
    {
      Label1.Text = "";
      Label2.Text = "";
      Label3.Text = "";
      Label4.Text = "";
    }

  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Page Validate</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      TextBox
      <asp:TextBox ID="TextBox1" ValidationGroup="Group1" runat="server"></asp:TextBox>
      <asp:RequiredFieldValidator Display="Static" ID="TextBoxValidator1" ValidationGroup="Group1" runat="server" ControlToValidate="TextBox1" ErrorMessage="(please enter some text)" EnableClientScript="False"></asp:RequiredFieldValidator>
      <br />
      CheckBox
      <asp:CheckBox ID="CheckBox1" ValidationGroup="Group2" runat="server" />
      <asp:CustomValidator ID="CustomValidator" ValidationGroup="Group2" runat="server" Text="(this option required)" OnServerValidate="CustomValidator_ServerValidate" EnableClientScript="False"></asp:CustomValidator>
      <br />
      <asp:Button ID="Button1" ValidationGroup="Group1" CausesValidation="true" runat="server" Text="Validate Group1 Controls" OnClick="Button_Click" />
      <asp:Label ID="Label1" runat="server"></asp:Label>
      <br />
      <asp:Button ID="Button2" ValidationGroup="Group2" CausesValidation="false" runat="server" Text="Validate Group2 Controls" OnClick="Button_Click" />
      <asp:Label ID="Label2" runat="server"></asp:Label>
      <br />
      <br />
      AutoPostBack TextBox
      <asp:TextBox AutoPostBack="true" ID="TextBox2" ValidationGroup="Group3" runat="server" CausesValidation="true"></asp:TextBox>
      <asp:RequiredFieldValidator ID="TextBoxValidator2" ValidationGroup="Group3" runat="server" ControlToValidate="TextBox2" ErrorMessage="(please enter some text)" EnableClientScript="False"></asp:RequiredFieldValidator>
      <asp:Label ID="Label3" runat="server"></asp:Label>
      <br />
      <br />
      <asp:Button ID="Button3" CausesValidation="true" runat="server" Text="Validate All Controls" OnClick="Button3_Click" />
      <asp:Label ID="Label4" runat="server"></asp:Label>
    
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>

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

<script runat="server">

  Protected Sub Button_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Select Case CType(sender, Button).ID
      Case "Button1"
        If TextBoxValidator1.IsValid Then
          Label1.Text = "TextBox validates."
        Else
          Label1.Text = ""
          Label4.Text = ""
        End If
      Case "Button2"
        ' Must explicitly cause Validate here because 
        ' Button2 has CausesValidation set to false.
        Validate("Group2")
        If CustomValidator.IsValid Then
          Label2.Text = "CheckBox validates."
        Else
          Label2.Text = ""
          Label4.Text = ""
        End If
      Case Else
        Label1.Text = ""
        Label2.Text = ""
    End Select

  End Sub
  
  Protected Sub CustomValidator_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
     
    args.IsValid = (CheckBox1.Checked)

  End Sub
  
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

    If (IsPostBack) Then
      
    ' Handle AutoPostBack TextBox.
      Validate("Group3")
      If (Page.IsValid) Then
        Label3.Text = "AutoPostBack TextBox validates."
      Else
        Label3.Text = ""
        Label4.Text = ""
      End If
    End If
    
  End Sub

  Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  
    Validate()
    If (Page.IsValid) Then
      Label4.Text = "All controls valid."
    Else
      Label1.Text = ""
      Label2.Text = ""
      Label3.Text = ""
      Label4.Text = ""
    End If
    
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Page Validate</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      TextBox
      <asp:TextBox ID="TextBox1" ValidationGroup="Group1" runat="server"></asp:TextBox>
      <asp:RequiredFieldValidator Display="Static" ID="TextBoxValidator1" ValidationGroup="Group1" runat="server" ControlToValidate="TextBox1" ErrorMessage="(please enter some text)" EnableClientScript="False"></asp:RequiredFieldValidator>
      <br />
      CheckBox
      <asp:CheckBox ID="CheckBox1" ValidationGroup="Group2" runat="server" />
      <asp:CustomValidator ID="CustomValidator" ValidationGroup="Group2" runat="server" Text="(this option required)" OnServerValidate="CustomValidator_ServerValidate" EnableClientScript="False"></asp:CustomValidator>
      <br />
      <asp:Button ID="Button1" ValidationGroup="Group1" CausesValidation="true" runat="server" Text="Validate Group1 Controls" OnClick="Button_Click" />
      <asp:Label ID="Label1" runat="server"></asp:Label>
      <br />
      <asp:Button ID="Button2" ValidationGroup="Group2" CausesValidation="false" runat="server" Text="Validate Group2 Controls" OnClick="Button_Click" />
      <asp:Label ID="Label2" runat="server"></asp:Label>
      <br />
      <br />
      AutoPostBack TextBox
      <asp:TextBox AutoPostBack="true" ID="TextBox2" ValidationGroup="Group3" runat="server" CausesValidation="true"></asp:TextBox>
      <asp:RequiredFieldValidator ID="TextBoxValidator2" ValidationGroup="Group3" runat="server" ControlToValidate="TextBox2" ErrorMessage="(please enter some text)" EnableClientScript="False"></asp:RequiredFieldValidator>
      <asp:Label ID="Label3" runat="server"></asp:Label>
      <br />
      <br />
      <asp:Button ID="Button3" CausesValidation="true" runat="server" Text="Validate All Controls" OnClick="Button3_Click" />
      <asp:Label ID="Label4" runat="server"></asp:Label>
    
    </div>
    </form>
</body>
</html>

注解

当用户单击已 CausesValidation 设置为 true属性的任何 ASP.NET 服务器控件(默认值)时,将调用此方法。 Button其中包括、LinkButtonImageButton和 Web 服务器控件、HtmlInputButtonHtmlButtonHtmlInputImage以及 HTML 服务器控件以及可自动帖子返回到服务器(如TextBoxCheckBoxListControl和控件)的BulletedList控件。

若要禁用页面上任何按钮控件的验证,请将按钮控件 CausesValidation 的属性设置为 false

该方法 Validate 验证指定的验证组。 在验证组上调用 Validate 该方法后, IsValid 仅当指定的验证组和导致页面发布到服务器的控件的验证组有效时,该方法才会返回 true

另请参阅

适用于