當自訂驗證器驗證 ControlToValidate 屬性的值時發生。
public event System.Web.UI.WebControls.ServerValidateEventHandler ServerValidate
備註
如果使用此事件登錄方法,則會使用 ControlToValidate 屬性值呼叫該方法。只有此事件處理常式傳回 true 時,驗證才成功完成。
範例
下列範例示範如何截獲 ServerValidate 事件以加入驗證頁面的邏輯。
[Visual Basic]
Protected Sub Submit_OnClick(sender As Object, e As EventArgs)
' Validator checking. If things go wrong, return.
' The validator's error message will be displayed on
' the same panel.
If Page.IsValid Then
ActiveForm = Form2
End If
End Sub
Public Sub ServerValidate(source As Object, args As ServerValidateEventArgs)
Dim num As Integer = Int32.Parse(args.Value)
If num Mod 2 = 0 Then
cust.Text = "Valid number"
args.IsValid = True
Else
cust.Text = "InValid number"
args.IsValid = False
End If
End Sub
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
cust.Alignment = System.Web.UI.MobileControls.Alignment.Center
cust.Wrapping = System.Web.UI.MobileControls.Wrapping.NoWrap
cust.ForeColor = System.Drawing.Color.Blue
cust.StyleReference = "Title"
cust.Display = System.Web.UI.WebControls.ValidatorDisplay.Static
End Sub
<mobile:Form id="Form1" runat=server>
<mobile:Label id="message1" runat=server>
Please enter an Even number
</mobile:Label>
<mobile:TextBox id="number" runat=server MaxLength=9 />
<mobile:CustomValidator ID="cust" ControlToValidate="number"
OnServerValidate="ServerValidate"
runat=server >
Invalid number
</mobile:CustomValidator>
<mobile:Command id="Command1" runat=server OnClick="Submit_OnClick">
Submit
</mobile:Command>
</mobile:Form>
<mobile:Form id="Form2" runat=server>
<mobile:Label id="Label2" runat=server>number is submitted</mobile:Label>
</mobile:Form>
<script language="c#" runat=server>
protected void Submit_OnClick(Object sender, EventArgs e)
{
// Validator checking. If things go wrong, return.
// The validator's error message will be displayed on
// the same panel.
if (Page.IsValid)
{
ActiveForm = Form2;
}
}
public void ServerValidate (object source, ServerValidateEventArgs args)
{
int num = Int32.Parse(args.Value);
if (num % 2 == 0)
{
cust.Text = "Valid number";
args.IsValid = true;
}
else
{
cust.Text = "InValid number";
args.IsValid = false;
}
}
public void Page_Load(Object sender, EventArgs e)
{
cust.Alignment = System.Web.UI.MobileControls.Alignment.Center;
cust.Wrapping = System.Web.UI.MobileControls.Wrapping.NoWrap;
cust.ForeColor = System.Drawing.Color.Blue;
cust.StyleReference = "Title";
cust.Display = System.Web.UI.WebControls.ValidatorDisplay.Static;
}
</script>
<mobile:Form id="Form1" runat=server>
<mobile:Label id="message1" runat=server>
Please enter an Even number
</mobile:Label>
<mobile:TextBox id="number" runat=server MaxLength=9 />
<mobile:CustomValidator ID="cust" ControlToValidate="number"
OnServerValidate="ServerValidate"
runat=server >
Invalid number
</mobile:CustomValidator>
<mobile:Command id="Command1" runat=server OnClick="Submit_OnClick">
Submit
</mobile:Command>
</mobile:Form>
<mobile:Form id="Form2" runat=server>
<mobile:Label id="Label2" runat=server>number is submitted</mobile:Label>
</mobile:Form>