Click here to Skip to main content
15,881,882 members
Articles / Web Development / XHTML

Synchronous PageMethod Custom Validator Control

Rate me:
Please Sign up or sign in to vote.
4.43/5 (4 votes)
5 May 2009CPOL2 min read 44.5K   669   23   16
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)


Written By
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

 
QuestionInjecting a debugger line into the javascript generated by this class Pin
AllenRogerMarshall2-Nov-15 9:24
professionalAllenRogerMarshall2-Nov-15 9:24 
QuestionNaming the parameter to the WebMethod Pin
AllenRogerMarshall2-Nov-15 8:46
professionalAllenRogerMarshall2-Nov-15 8:46 
QuestionI have found that the httpContext.resultText is an object, not a simple type Pin
AllenRogerMarshall2-Nov-15 8:35
professionalAllenRogerMarshall2-Nov-15 8:35 
QuestionWebMethodRunCondition Pin
Karlo Medallo28-Jun-12 15:06
Karlo Medallo28-Jun-12 15:06 
AnswerRe: WebMethodRunCondition Pin
AllenRogerMarshall2-Nov-15 8:47
professionalAllenRogerMarshall2-Nov-15 8:47 
GeneralDemo does not work Pin
dalej6-May-09 6:34
dalej6-May-09 6:34 
GeneralRe: Demo does not work Pin
Greg Olmstead6-May-09 7:14
Greg Olmstead6-May-09 7:14 
GeneralRe: Demo does not work Pin
MarkLF23-May-09 23:58
MarkLF23-May-09 23:58 
GeneralRe: Demo does not work Pin
Member 262340213-Apr-11 20:32
Member 262340213-Apr-11 20:32 
GeneralDoes not show error message Pin
MarkLF5-May-09 4:58
MarkLF5-May-09 4:58 
GeneralThanks and request Pin
MarkLF4-May-09 3:35
MarkLF4-May-09 3:35 
GeneralRe: Thanks and request Pin
Greg Olmstead4-May-09 4:22
Greg Olmstead4-May-09 4:22 
GeneralRe: Thanks and request Pin
MarkLF4-May-09 21:02
MarkLF4-May-09 21:02 
GeneralIt is really works? Pin
steeve.gauvreau3-Aug-11 3:46
steeve.gauvreau3-Aug-11 3:46 
GeneralRe: It is really works? Pin
AllenRogerMarshall2-Nov-15 8:48
professionalAllenRogerMarshall2-Nov-15 8:48 
GeneralValidation Pin
Artiom Chilaru4-May-09 0:29
Artiom Chilaru4-May-09 0:29 

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

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