How to Add an Error Message to a ValidationSummary Progrommatically

For a while now I had been annoyed at the fact that there is no easy way to add an error message to a ValidationSummary control... Or at least I didn't think that there was, but it turns out that it's quite simple:

            RequiredFieldValidator Validator = new RequiredFieldValidator();
            Validator.ErrorMessage = "This is your error message";
            Validator.ValidationGroup = "Group1";
            Validator.IsValid = false;
            Validator.Visible = false;
            Page.Form.Controls.Add(Validator);

The error message and the validation group needs to be changed but basically this just creates a required field validator. It then says that the validator isn't valid and makes it invisible. However since we have a ValidationSummary object, it will show up there (assuming we set up the validation group properly). That's all there is to it. Anyway, I hope this helps out someone so you're not banging your head against a wall like I was. So try it out, leave feedback, and happy coding.

kick it on DotNetKicks.com   Shout it
Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkListEmail

Posted by: James Craig
Posted on: 7/21/2009 at 10:15 AM
Tags: , , ,
Categories: ASP.Net
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading