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) |