指示该页上包含的所有验证控件验证指派给它们的信息。
程序集: System.Web(在 System.Web.dll 中)
用户单击任何将 CausesValidation 属性设置为 true(为默认值)的 ASP.NET 服务器控件时调用此方法。 这些控件包括:Button、ImageButton 和 LinkButton Web 服务器控件;HtmlInputButton、HtmlInputImage 和 HtmlButton HTML 服务器控件;以及可自动回发到服务器的控件(如 TextBox、CheckBox、ListControl 和 BulletedList 控件)。
若要禁用对页上的任何按钮控件的验证,请将按钮控件的 CausesValidation 属性设置为 false。
调用此方法时,它将循环访问与 Page
注意 |
|---|
页验证的行为已更改。 在 ASP.NET 2.0 中,控件不再调用 Page |
ASP.NET 2.0 不使用 Validate 方法。 在使用 ASP.NET 2.0 时,重写 Validate(String) 方法以更改页验证行为。
下面的代码示例对定义了几个不同验证组的方案的页调用 Validate 方法。
安全说明 |
|---|
此示例有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 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>
<%@ 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>
Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2
.NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。
注意
安全说明