ServerValidate, événement
Se produit lorsque le validateur personnalisé valide la valeur de la propriété ControlToValidate.
public event System.Web.UI.WebControls.ServerValidateEventHandler ServerValidate
Notes
Si une méthode est inscrite auprès de cet événement, elle est appelée avec la valeur de la propriété ControlToValidate. La validation ne réussit que si le gestionnaire d'événements retourne true.
Exemple
L'exemple suivant montre comment utiliser l'événement ServerValidate pour ajouter une logique permettant de valider la page.
[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>
Voir aussi
CustomValidator, classe