web analytics

How to use CustomValidator control to provide a user-defined validation in ASP.NET?

Options

codeling 1602 - 6666
@2016-01-27 09:04:31

Use the CustomValidator control to provide a user-defined validation function for an input control. The CustomValidator control is a separate control from the input control it validates, which allows you to control where the validation message is displayed.

Validation controls always perform validation on the server. They also have complete client-side implementation that allows script-enabled browsers to perform validation on the client. Client-side validation enhances the validation process by checking user input before it is sent to the server. This allows errors to be detected on the client before the form is submitted, thus avoiding the round trip of information necessary for server-side validation.

To create a server-side validation function, provide a handler for the ServerValidate event that performs the validation. The string from the input control to validate can be accessed by using the Value property of the ServerValidateEventArgs object passed into the event handler as a parameter. The result of the validation is then stored in the IsValid property of the ServerValidateEventArgs object.

It is possible to use a CustomValidator control without setting the ControlToValidate property. This is commonly done when you are validating multiple input controls or validating input controls that cannot be used with validation controls, such as the CheckBox control. In this case, the Value property of the arguments parameter passed to the event handler for the ServerValidate event and to the client-side validation function always contains an empty string (""). However, these validation functions are still called, where appropriate, to determine validity on both the server and client. To access the value to validate, you must programmatically reference the input control you want to validate and then retrieve the value from the appropriate property. For example, to validate a CheckBox control on the server, do not set the ControlToValidate property of the validation control and use the following code for the handler for the ServerValidate event.

The following code example demonstrates how to create a server-side CustomValidator control.

<%@ Page Language="C#" AutoEventWireup="True" %>


<!DOCTYPE html>

<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" />

      &nbsp;&nbsp;
      <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>

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com