Share via


ServerValidate Event

Occurs when the custom validator validates the value of the ControlToValidate property.

public event System.Web.UI.WebControls.ServerValidateEventHandler ServerValidate

Remarks

If a method is registered with this event, it is called with the value of the ControlToValidate property. Validation succeeds only if this event handler returns true.

Example

The following example demonstrates how to trap the ServerValidate event to add logic to validate the 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>

See Also

CustomValidator Class