DCSIMG
Quick Tip – Adding Business Validation Messages to ValidationSummary - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2013 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Quick Tip – Adding Business Validation Messages to ValidationSummary

Quick Tip – Adding Business Validation Messages to ValidationSummary

Two days ago I was askedAdding Business Validations to ValidationSummary
how to insert business
validation messages into a
ValidationSummary control.
The post is the answer I
gave to the person who asked
me.

The Problem

We have a web form that is doing input validation and output the errors
inside a ValidationSummary control. Since ValidationSummary shows a
summary of all validation errors from ASP.NET validators how can we
add business validations which are part of the business logic we have to
the output of the ValidationSummary ?
Also, how can we make the input not valid according to the business logic?

The Answer

We can use the CustomValidator control to our rescue. In code we will
create the CustomValidator and sets its IsValid property to false. We
also add our business error message to the ErrorMessage property.
Then we will add the created CustomValidator to the Page’s Validator
list. The following helper method will perform what I describe:

public static void AddBusinessErrorMessage(Page page, string message)
{
    var validator = new CustomValidator();
    validator.IsValid = false;
    validator.ErrorMessage = message;
    page.Validators.Add(validator);
}

Summary

In the post I showed an example of using a CustomValidator in order
to add business error messages to a ValidationSummary control.
There are more solutions out there that will do the same thing but this
solution is the easiest to implement.

DotNetKicks Image

Comments

Quick Tip ??? Adding Business Validations Messages to … « Noya Khobor said:

Pingback from  Quick Tip ??? Adding Business Validations Messages to … «  Noya Khobor

# September 11, 2009 8:32 PM

Quick Tip ??? Adding Business Validations Messages to … | kozmom news said:

Pingback from  Quick Tip ??? Adding Business Validations Messages to … | kozmom news

# September 11, 2009 9:52 PM

Quick Tip ??? Adding Business Validations Messages to … - penawebid said:

Pingback from  Quick Tip ??? Adding Business Validations Messages to … - penawebid

# September 11, 2009 10:13 PM

Quick Tip ??? Adding Business Validations Messages to … | 1001need News said:

Pingback from  Quick Tip ??? Adding Business Validations Messages to … | 1001need News

# September 11, 2009 10:35 PM