Putting Messages Into a ValidationSummary Control...From Code

by ashic 11. December 2008 14:49

I love the asp.net validation controls. They're chick and really useful. We sometimes have to show some messages that are not the ErrorMessages of validators. An approach to do this could be to

a) ScriptManager.RegisterClientScriptBlock to show a popup (YUCK!!)

b) Put in a label and set its Text from code. This could be an option, only the error message will be in the label and not inside the ValidationSummary. This does not look good as some errors will be shown neatly in a ValidationSummary whereas others will eb shown in a separate label.

c) This is my favourite. Here's what you do:

In code behind, when you want to add a message to the ValidationSummary, just do this:

RequiredFieldValidator req = new RequiredFieldValidator();
req.ValidationGroup = "vgInput";
req.ErrorMessage = "Your message goes here.";
req.IsValid = false;
Page.Form.Controls.Add(req);
req.Visible = false;

Notice the last three lines. One causes page validation to fail, the next adds the validator to the page's controls so it can have effect (validators must be inside the form tag). The last hides the validator so the error message is shown only in the ValidationSummary and not at the location of the validator (which is at the end of the form as it's added dynamically.

Hope that helps.

Share or Bookmark this post…
  • DotNetKicks
  • LinkedIn
  • Technorati
  • msdn Social
  • del.icio.us
  • Digg
  • Reddit
  • StumbleUpon
  • Slashdot
  • Live
  • Google
  • Facebook
Categories: ASP.NET

Comments

1/30/2009 8:23:23 AM #

http://

Awesome.

http://

2/21/2009 1:52:11 PM #

http://

32423

http://

10/17/2009 2:49:18 AM #

how to beat a drug test

Now this is hghly recommeded post for me. I will surely email this to my friend.


Regards

Adom

how to beat a drug test United States

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading




Powered by BlogEngine.NET 1.5.1.29
Theme by Ashic Mahtab

Stats