ServerValidateEventArgs 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为 ServerValidate 控件的 CustomValidator 事件提供数据。 此类不能被继承。
public ref class ServerValidateEventArgs sealed : EventArgs
public ref class ServerValidateEventArgs : EventArgs
public sealed class ServerValidateEventArgs : EventArgs
public class ServerValidateEventArgs : EventArgs
type ServerValidateEventArgs = class
inherit EventArgs
Public NotInheritable Class ServerValidateEventArgs
Inherits EventArgs
Public Class ServerValidateEventArgs
Inherits EventArgs
- 继承
示例
下面的示例演示如何使用 ServerValidateEventArgs 将事件数据传递给 事件的自定义事件处理程序 ServerValidate 。 请注意, Value 的 ServerValidateEventArgs 属性用于确定要验证的值,而 IsValid 属性用于存储验证结果。
<%@ 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>
<%@ 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;
}
}
void Page_Load(object sender, EventArgs e)
{
// Manually register the event-handling method for the
// ServerValidate event of the CustomValidator control.
CustomValidator1.ServerValidate +=
new ServerValidateEventHandler(this.ServerValidation);
}
</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"
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
Sub Page_Load(sende As object, e As EventArgs)
' Manually register the event-handling method for the
' ServerValidate event of the CustomValidator control.
AddHandler CustomValidator1.ServerValidate, _
AddressOf ServerValidation
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"
runat="server"/>
<br />
<asp:Button id="Button1"
Text="Validate"
OnClick="ValidateBtn_OnClick"
runat="server"/>
</form>
</body>
</html>
注解
ServerValidateEventArgs将 传递给事件处理程序,ServerValidate以便向处理程序提供事件数据。 在 ServerValidate 服务器上执行验证时引发 事件。 这使你可以对输入控件的值执行自定义服务器端验证例程, CustomValidator (与事件处理程序中) 与之关联的控件。
要验证的值通过使用 Value 属性确定。 代码确定值是否有效后,将验证结果存储在 属性中 IsValid 。
有关 实例 ServerValidateEventArgs的初始属性值的列表, ServerValidateEventArgs 请参阅 构造函数。
有关如何处理事件的详细信息,请参阅 处理和引发事件。
构造函数
ServerValidateEventArgs(String, Boolean) |
初始化 ServerValidateEventArgs 类的新实例。 |
属性
IsValid |
获取或设置由 Value 属性指定的值是否通过验证。 |
Value |
获取要在 ServerValidate 事件的自定义事件处理程序中验证的值。 |
方法
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |