CustomValidator.ServerValidate 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在服务器上执行验证时发生。
public:
event System::Web::UI::WebControls::ServerValidateEventHandler ^ ServerValidate;
public event System.Web.UI.WebControls.ServerValidateEventHandler ServerValidate;
member this.ServerValidate : System.Web.UI.WebControls.ServerValidateEventHandler
Public Custom Event ServerValidate As ServerValidateEventHandler
事件类型
示例
下面的代码示例演示如何指定和编码服务器端 CustomValidator 控件的处理程序。 它创建自定义验证例程,验证控件的值 TextBox 是否为偶数,然后显示验证结果。
重要
此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述。
<%@ Page Language="C#" AutoEventWireup="True" %>
<!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>
<title>CustomValidator ServerValidate Example</title>
<script runat="server">
void ValidateBtn_OnClick(object sender, EventArgs e)
{
// Display whether the page passed validation.
if (Page.IsValid)
{
Message.Text = "Page is valid.";
}
else
{
Message.Text = "Page is not valid!";
}
}
void ServerValidation(object source, ServerValidateEventArgs args)
{
try
{
// Test whether the value entered into the text box is even.
int i = int.Parse(args.Value);
args.IsValid = ((i%2) == 0);
}
catch(Exception ex)
{
args.IsValid = false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>CustomValidator ServerValidate Example</h3>
<asp:Label id="Message"
Text="Enter an even number:"
Font-Names="Verdana"
Font-Size="10pt"
runat="server"
AssociatedControlID="Text1"/>
<br />
<asp:TextBox id="Text1"
runat="server" />
<asp:CustomValidator id="CustomValidator1"
ControlToValidate="Text1"
Display="Static"
ErrorMessage="Not an even number!"
ForeColor="green"
Font-Names="verdana"
Font-Size="10pt"
OnServerValidate="ServerValidation"
runat="server"/>
<br />
<asp:Button id="Button1"
Text="Validate"
OnClick="ValidateBtn_OnClick"
runat="server"/>
</form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<!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>
<title>CustomValidator ServerValidate Example</title>
<script runat="server">
Sub ValidateBtn_OnClick(sender As Object, e As EventArgs)
' Display whether the page passed validation.
If Page.IsValid Then
Message.Text = "Page is valid."
Else
Message.Text = "Page is not valid!"
End If
End Sub
Sub ServerValidation(source As Object, args As ServerValidateEventArgs)
Try
' Test whether the value entered into the text box is even.
Dim num As Integer = Integer.Parse(args.Value)
args.IsValid = ((num mod 2) = 0)
Catch ex As Exception
args.IsValid = false
End Try
End Sub
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>CustomValidator ServerValidate Example</h3>
<asp:Label id="Message"
Text="Enter an even number:"
Font-Names="Verdana"
Font-Size="10pt"
runat="server"
AssociatedControlID="Text1" />
<br />
<asp:TextBox id="Text1"
runat="server" />
<asp:CustomValidator id="CustomValidator1"
ControlToValidate="Text1"
Display="Static"
ErrorMessage="Not an even number!"
ForeColor="green"
Font-Names="verdana"
Font-Size="10pt"
OnServerValidate="ServerValidation"
runat="server"/>
<br />
<asp:Button id="Button1"
Text="Validate"
OnClick="ValidateBtn_OnClick"
runat="server"/>
</form>
</body>
</html>
注解
在 ServerValidate 服务器上执行验证时,将引发 事件。 此事件用于为输入控件(例如 TextBox 控件)提供自定义验证例程。
有关处理事件的详细信息,请参阅 处理和引发事件。