Page.Validate 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指示该页上包含的所有验证控件验证指派给它们的信息。
重载
| 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>
注解
当用户单击属性设置为 true的任何 ASP.NET 服务器控件CausesValidation(默认值)时,将调用此方法。 这些控件包括 Button、 ImageButton和 LinkButton Web 服务器控件、 HtmlInputButton、 HtmlInputImage和 HtmlButton HTML 服务器控件,以及可自动发回服务器的控件,例如 TextBox、 CheckBox、 ListControl和 BulletedList 控件。
若要对页面上的任何按钮控件禁用验证,请将按钮控件的 CausesValidation 属性设置为 false。
调用此方法时,它会循环访问与 Page.Validators 属性关联的 对象中包含的ValidatorCollection验证控件,并为当前验证组中的每个验证控件调用验证逻辑。 验证组由将页面发布到服务器的控件确定。 如果未指定验证组,则不会使用任何验证组。
注意
页面验证的行为已更改。 在 ASP.NET 2.0 中,控件不再调用 Page.Validate() 方法;它们改用 Page.Validate(String) 方法。 如果在 ASP.NET 2.0 页上使用 Page.Validate() 方法,则会忽略验证组并验证所有控件。
继承者说明
Validate() ASP.NET 2.0 不使用 方法。 使用 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>
注解
当用户单击属性设置为 true的任何 ASP.NET 服务器控件CausesValidation(默认值)时,将调用此方法。 这些控件包括 Button、 ImageButton和 LinkButton Web 服务器控件、 HtmlInputButton、 HtmlInputImage和 HtmlButton HTML 服务器控件,以及可自动发回服务器的控件,例如 TextBox、 CheckBox、 ListControl和 BulletedList 控件。
若要对页面上的任何按钮控件禁用验证,请将按钮控件的 CausesValidation 属性设置为 false。
方法 Validate 验证指定的验证组。 在验证组上调用 Validate 方法后, IsValid 仅当指定的验证组和导致页面发布到服务器的控件的验证组都有效时,该方法才会返回 true 。