Click here to Skip to main content
Click here to Skip to main content

Synchronous PageMethod Custom Validator Control

, 5 May 2009
Rate this:
Please Sign up or sign in to vote.
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:

[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)

About the Author

Greg Olmstead
Founder SparkIT Solutions
Canada Canada
My formal education is in Network Engineering and Security Analysis, but I have been programming for about four years. My specialty is ASP.NET, C#, WCF, and WPF. I live in Ontario, Canada.
 
SparkIT Solutions provides customized software and hardware solutions for small to large businesses. Check us out at http://www.sparkits.ca

Comments and Discussions

 
QuestionWebMethodRunCondition PinmemberKarlo Medallo28-Jun-12 15:06 
GeneralDemo does not work Pinmemberdalej6-May-09 6:34 
GeneralRe: Demo does not work PinmemberGreg Olmstead6-May-09 7:14 
GeneralRe: Demo does not work PinmemberMarkLF23-May-09 23:58 
GeneralRe: Demo does not work PinmemberMember 262340213-Apr-11 20:32 
GeneralDoes not show error message PinmemberMarkLF5-May-09 4:58 
GeneralThanks and request PinmemberMarkLF4-May-09 3:35 
GeneralRe: Thanks and request PinmemberGreg Olmstead4-May-09 4:22 
GeneralRe: Thanks and request PinmemberMarkLF4-May-09 21:02 
GeneralIt is really works? Pinmembersteeve.gauvreau3-Aug-11 3:46 
GeneralValidation PinmemberArtiom Chilaru4-May-09 0:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 5 May 2009
Article Copyright 2009 by Greg Olmstead
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid