My Profile

Thursday, February 10, 2011

ASP.Net - CustomValidator



1.    CustomValidator:
·       If all above four validation controls doesn’t fulfill our requirement then we have to use Customvalidator and write our own logic.
·       Again for client side validations, we have to write javascript code and for server side validations, we have to write C# code.

How to use CustomValidator for Client side validations?

·       Place Customvalidator and set the following properties:
 ControlToValidate       :
 ErrorMessage     :
 ClientValidationFunction*   : <JS func name>

·       Goto source view and implement JS function inside <script> tag.

This function should be specified with two arguments just like any .net event procedure arguments.
·        
Write code in this function to perform validation and return Yes/No to CustomValidator.
Example JS function:
<script type=”text/javascript”>
     function CheckQty(x , y)
     {
              If(y.Value % 5 == 0)
                        y.IsValid = true;
              else
                        y.IsValid = false;
     }
</script>

How to use CustomValidator for Server side Validations?

Server side validations means, when we submit the form it will be executed at server side. If any other code is there then as usual server will run that code also.

To find whether validation in the current page is successfully validated, we can use “Page.IsValid” property. This property returns true, if all validation controls in the page are successful. Otherwise, it returns false.
Follow the following steps to check this…

·       Place CustomValidator in the form and set the regular properties like:
 ControlToValidate :
 Errormessage  :
And other common properties…
·       Goto Properties window and in Events tab, select “ServerValidate” event. Double click on it; it will display a function in code window, where we have to write the validation code. Again using two arguments which are provided by even procedure or function.

While working with CustomValidator for server side validations and client side validations we have to be clear in points, those are:

·       How it is executed? **
·       For any reason submit doesn’t happen then server side validation will not execute.
·       When we submit the form to server, validation code will run but also other code (if any) will also be executed.

Let’s see an example to make it clear.

Example:
Design the form as following


·       Place a CustomValidator control beside textbox and set the following Properties
  ControlToValidate   :   TextBox1
  ErrorMessage            :  Quantity must be Below the Qty    Available.
  ForeColor                  :  Red

·       Goto Events of that CustomValidator and double click on “ServerValidate” event and write the following code in the function displayed.
if (Convert.ToInt32(args.Value) > 500)
            args.IsValid = false;
         else
            args.IsValid = true;

·        Under Button Click event:

if (Page.IsValid)
            Response.Write("Order Placed.....");
          else
            Response.Write("Please correct Errors!");




Yes! We are done with all Validation controls. Hope you all can do any validation in web forms now.

 
---------------------------------------------------------- 
** If you like this post please like our page in 
facebook " Dot Net Support"


0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More