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 Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。
<%@ 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 控件。
如需處理事件的詳細資訊,請參閱 處理和引發事件。