Click here to Skip to main content
13,256,716 members (50,281 online)
Click here to Skip to main content
Add your own
alternative version


37 bookmarked
Posted 4 Mar 2005

Adding ASP.NET validation at runtime

, 4 Mar 2005
Rate this:
Please Sign up or sign in to vote.
An article describing how to add errors to the validation summary at runtime

Runtime validation

<!------------------------------- STEP 3 ---------------------------><!-- Add the article text. Please use simple formatting (


etc) -->


This article first appeared on

This article explains how to add error messages to a ValidationSummary at runtime and have the addition of those message set Page.IsValid to False. This technique is useful when you do not know until runtime what constraints should apply to a webform, or when you want to validate against constraints which do not relate directly to a WebControl.

One of the requirements when writing was the ability to model object constraints in OCL and then to validate the current page against those constraints.

The problem with normal validators is that they are designed to validate individual controls rather than a list of constraints on an object. The approach I took was to create a validator which I can place on any web form and add as many errors to as I required.

The first step was to create a WebControl which supported IValidator

public class MultiValidator : WebControl,  IValidator

I then added an ArrayList to hold the error strings, and a method to add an error.

private ArrayList Errors = new ArrayList();

public void AddError(string message)

When ASP.NET validates a page it enumerates all IValidators within its own Validators property, and called IValidator.Validate(). To determine if the page is valid or not it then checks IValidator.IsValid.

To add custom error messages at runtime I decided to create a static validator class which always returns "false" from IsValidator.IsValid. For each error message in my MultiValidator I could then simply create an instance of one of these validators.

internal class StaticValidator : IValidator
    private string errorMessage;

    #region IValidator
    void IValidator.Validate()

    bool IValidator.IsValid
        get { return false; }
        set { }

    public string ErrorMessage
        get { return errorMessage; }
        set { errorMessage = value; }

Now that the StaticValidator was written, all I needed to do was to add the required IValidator implementations to my MultiValidator class.

#region IValidator
void IValidator.Validate()
    isValid = (Errors.Count == 0);
    foreach(string error in Errors)
        StaticValidator validator = new StaticValidator();
        validator.ErrorMessage = error;
    }//foreach errors

bool IValidator.IsValid
    get { return isValid; }
    set { isValid = value; }

Within a webform, I would now

  1. Set CausesValidation to false on my submit button.
  2. Validate my object.
  3. Call MultiValidator1.AddError() for each error encountered.
  4. Call Page.Validate();
  5. Check Page.IsValid as normal.
Using a ValidationSummary I could then display the broken constraints to the user for rectification.

Using the code

To use the code, simply compile the two provided CS files into a WebControl assembly and then drop a MultiValidator onto your webpage. Errors may be added like so

if (SomeUnusualCondition)
  multiValidator1.AddError("Something unusual is wrong");


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Peter Morris
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralHere is the sample Pin
Member 323276626-Dec-08 3:13
memberMember 323276626-Dec-08 3:13 
GeneralVery Strange behaviour [modified] Pin
Mogaambo31-May-08 5:34
memberMogaambo31-May-08 5:34 
AnswerVB Version Pin
Paul Appeldoorn6-Mar-08 2:43
memberPaul Appeldoorn6-Mar-08 2:43 
GeneralMultiValidator1 doesn't appear on the webform Pin
eyale30-Apr-07 10:52
membereyale30-Apr-07 10:52 
GeneralI need your Help Pin
Ronjon Kumar Ghosh21-Jan-07 19:17
memberRonjon Kumar Ghosh21-Jan-07 19:17 
Hello Morris
I am trying your code. My clients requirement is to show a message after successfully done any db oepration in the same window where i show all validation messagei used here asp:ValidationSummary control. now i palced your MultiValidator control in my page. in code behind i call MultiValidator1.AddError(message) and check that whether page.IsValid is false. but unfortunately validation summary messagebox will not appear.
Is there any other process i Missed? will u plz help me how can I Do this ?


Questionregular expression in the custom control. Pin
karthik000713-Sep-06 2:38
memberkarthik000713-Sep-06 2:38 
Generalu need to provide full length example on usage of ur code/dll created. Pin
c-Mohan15-Apr-06 6:44
memberc-Mohan15-Apr-06 6:44 
GeneralRe: u need to provide full length example on usage of ur code/dll created. Pin
Mike Ellison4-Oct-06 13:15
memberMike Ellison4-Oct-06 13:15 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 4 Mar 2005
Article Copyright 2005 by Peter Morris
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid