Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / XHTML

Synchronous PageMethod Custom Validator Control

4.43/5 (4 votes)
5 May 2009CPOL2 min read 45.8K   670  
This custom validator will allow you to call your PageMethods to validate your client-side input, and block postbacks if there is an error.

Introduction

This control allows you to provide user-friendly validation of items which require server-side code. It allows you to validate these items without postback using PageMethods and JSON.

Background

This article is the result of two great articles which contributed very heavily to this one. The first, from Artiom Chilaru, was the article on using PageMethods in validators. The problem there was that the validator was asynchronous, and therefore was very difficult to use in a situation where you must block a postback if there are any validation errors.

The second was from Sandip Patel, about Synchronous AJAX calls. This was the missing link I needed to build this control to do what I want it to do, which was to fit into the way other validators work, and block postbacks on a client side validation error.

Using the code

This control can be added directly to any AJAX-enabled website project. I recommend adding it to a separate control library project in order to easily put it in your toolbox without depending on a compiled version of your website.

Once added to your page, you should fill in the WebMethod and ControlToValidate properties. After that, fill in the rest to bring it in line with the rest of your client-side validation style, such as ErrorMessage and Display.

You should then create the appropriate PageMethod in your code-behind. Remember to add the appropriate references and using statements. For a guide on PageMethods, go here.

There are two important points about creating a PageMethod. One, it must return a boolean, indicating whether the check passed or not. Second, it must accept a single string parameter called "value". Here is an example:

C#
[WebMethod]
public static bool ValidateUser(string value)
{
    return BaseClass.ValidateUsername(value, GetProject());
}

The name of the method should match what you specified in the WebMethod property of the validator.

Personally, I hand off most of these to a static BLL class which performs the actual validation. This is a good idea if your site has many pages which all use similar or the same validation rules.

Points of Interest

I have made a modification to the original article by Artiom to allow this to work with Cookie-less sessions. Essentially, it sets the path used to access the PageMethod to exclude the session key.

I want to once again thank Artiom and Sandip for their excellent articles. They helped me greatly, and I am sure they will help many people in the future.

A good scenario to use this control is when you have things like SpellCheckers which open on postback, and you don't wish to have to come back to the form with a validation error after the postback, other than for very exceptional situations.

I hope this control helps you with your own projects, as it has helped me with mine.

History

  • Version 1.0 - Published to The Code Project.
  • 2009 05 05 - Added demo project

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)