Click here to Skip to main content
15,880,891 members
Articles / Web Development / ASP.NET
Article

A Better ValidationSummary Control for Data Driven Applications

Rate me:
Please Sign up or sign in to vote.
3.71/5 (17 votes)
10 Jun 20022 min read 159.8K   782   39   28
The ValidationSummary Control can be easily subclassed to make data driven web applications more code-friendly.

Introduction

The validation controls that ship with ASP.NET go a long way towards reducing a lot of the tedious code you write in web pages. Already people are building on the concept of validation controls and providing customized versions to support specific data types that are common in many web applications. I want to demonstrate how a simple customization of the Validation Summary control can greatly clean up the code in your data driven web applications

Despite the power of the validation controls there are some validations that cannot ever be preformed on the client side. For example, validating that the user isn't entering duplicate information into your database. The CustomValidator control is made for our 'custom' code that must validate data entry but it is rather awkward because of the event model and concurrency issues inherent with web applications. Look at the code below to see what I mean. Notice we end up with a rather useless ServerValidate event handler. The only reason this can work is because the ValidationSummary control is smart enough not to render blank entries. Even though our CustomValidator is always invalid the output will still be normal because our custom validator will have a blank error message when everything is valid.

C#
private void CustomValidator1_ServerValidate(object source, 
    System.Web.UI.WebControls.ServerValidateEventArgs args) {

    /* Since our 'custom validation' occurs at the database level we can't
    really do anything useful here.  If we checked our database in here for
    duplicates we'd be requiring a database call and plus there could still be
    the chance that another user inserts their data in-between the time we made that
    call and the time we actually did our insert.  Always return false so that our
    real validation code has a chance to set the error message */

    args.IsValid = false;
}

private void SaveButton_Click(object sender, System.EventArgs e) {    

    /* This is where we insert a new record into the database.
    We will get an exception from the data provider if we
    are going to violate any database rules.  If we get that
    exception we want to put a message in the validation 
    summary */

    try {
        BusinessLayer.InsertIntoDatabase(InputField.Text);
        Response.Redirect("success.aspx");
    }
    catch (SqlException ex){
        if (ex.Number == 2601){
            CustomValidator1.ErrorMessage = "That input already exists, try another";
        }
    }

    // All other exceptions are 'unexpected' so let global error handler 
    // deal with them

}

After writing this code a few times I figured there must be a better way. I then found the solution by simply subclassing the ValidationSummary control. With my custom control I'm able to programmatically add error messages whenever I need. The resulting code is much cleaner:

C#
private void SaveButton_Click(object sender, System.EventArgs e) {    

    try {
        BusinessLayer.InsertIntoDatabase(InputField.Text);
        Response.Redirect("success.aspx");
    }
    catch (SqlException ex){
        if (ex.Number == 2601){
            CustomSummary.AddErrorMessage("That input already exists, try another");
        }
    }

}
The CustomSummary variable is an instance of my custom ValidationSummary control. Internally the AddErrorMessage is creating an internal class instance that implements the IValidator interface and adding it to the Page's Validators collection. Now that we haven't created a new validator the base behavior of the ValiationSummary control does the real work by iterating this collection and displaying the messages of instances marked invalid.
C#
namespace Juranek.Web.Controls {
    /// <summary>
    /// This control inheirits the ASP.NET ValidationSummary control but adds
    /// the ability to dynamically add error messages without requiring 
    /// validation controls.
    /// </summary>
    public class ValidationSummary : System.Web.UI.WebControls.ValidationSummary
    {
        /// <summary>
        /// Allows the caller to place custom text messages inside the validation
        /// summary control
        /// </summary>
        /// lt;param name="msg">The message you want to appear in the summary</param>
        public void AddErrorMessage(string msg) {
            this.Page.Validators.Add(new DummyValidator(msg));
        }
    }

    /// <summary>
    /// The validation summary control works by iterating over the Page.Validators
    /// collection and displaying the ErrorMessage property of each validator
    /// that return false for the IsValid() property.  This class will act 
    /// like all the other validators except it always is invalid and thus the 
    /// ErrorMessage property will always be displayed.
    /// </summary>
    internal class DummyValidator : IValidator {

        private string errorMsg;

        public DummyValidator(string msg) {
            errorMsg = msg;
        }

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

        public bool IsValid{
            get{return false;}
            set{}
        }

        public void Validate() {
        }
    }

}

Overall with just a little bit of code you can make a much more intuitive ValidationSummary control.

License

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


Written By
Chief Technology Officer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralHere is the Sample Pin
techbrij26-Dec-08 2:10
techbrij26-Dec-08 2:10 
Questionproblem with validationgroup Pin
work-shop25-Apr-07 1:44
work-shop25-Apr-07 1:44 
AnswerRe: problem with validationgroup Pin
Jason Meckley8-Jun-07 7:44
Jason Meckley8-Jun-07 7:44 
GeneralRe: problem with validationgroup Pin
work-shop10-Jun-07 22:54
work-shop10-Jun-07 22:54 
GeneralRe: problem with validationgroup Pin
work-shop24-Jun-07 23:00
work-shop24-Jun-07 23:00 
GeneralRe: problem with validationgroup Pin
work-shop24-Jun-07 23:53
work-shop24-Jun-07 23:53 
GeneralRe: problem with validationgroup Pin
work-shop25-Jun-07 0:03
work-shop25-Jun-07 0:03 
General[Message Removed] Pin
immetoz4-Oct-08 15:05
immetoz4-Oct-08 15:05 
AnswerRe: problem with validationgroup Pin
Robin van der Knaap27-Nov-07 23:45
Robin van der Knaap27-Nov-07 23:45 
QuestionBest Practice ? Pin
stixoffire22-Mar-07 20:50
stixoffire22-Mar-07 20:50 
AnswerRe: Best Practice ? Pin
Jason Meckley8-Jun-07 4:25
Jason Meckley8-Jun-07 4:25 
GeneralRe: Best Practice ? Pin
stixoffire8-Jun-07 12:27
stixoffire8-Jun-07 12:27 
GeneralIt didn't work Pin
A.N.Zayed2-Jan-07 0:30
A.N.Zayed2-Jan-07 0:30 
GeneralOther Approach Pin
timluyten4-Sep-06 23:09
timluyten4-Sep-06 23:09 
GeneralRe: Other Approach Pin
User 104326418-Apr-07 5:40
User 104326418-Apr-07 5:40 
QuestionRe: Other Approach Pin
stixoffire8-Jun-07 12:30
stixoffire8-Jun-07 12:30 
GeneralTo note [modified] Pin
Noisebox1-Jun-06 23:51
Noisebox1-Jun-06 23:51 
GeneralMessage Box Pin
Anonymous18-May-04 5:51
Anonymous18-May-04 5:51 
GeneralA sample web page would be useful Pin
RandallM4-Feb-04 14:33
RandallM4-Feb-04 14:33 
GeneralRe: A sample web page would be useful Pin
Idsa11-Apr-08 8:50
Idsa11-Apr-08 8:50 
Generalerror Pin
notorious r.o.b.25-Sep-03 10:27
notorious r.o.b.25-Sep-03 10:27 
GeneralRe: error Pin
notorious r.o.b.26-Sep-03 8:24
notorious r.o.b.26-Sep-03 8:24 
GeneralRe: error Pin
notorious r.o.b.26-Sep-03 8:25
notorious r.o.b.26-Sep-03 8:25 
GeneralCan't get rid of error message... Pin
Anonymous9-Aug-03 8:52
Anonymous9-Aug-03 8:52 
AnswerRe: Can't get rid of error message... Pin
intrader21-Feb-07 14:34
intrader21-Feb-07 14:34 
Is it possible that IsValid is not set to true?

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.