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…
  • Facebook
  • DotNetKicks
  • Digg
  • LinkedIn
  • Technorati
  • del.icio.us
  • Google
  • Live
  • Tumblr
  • msdn Social
  • Ping.fm
  • Reddit
  • Slashdot
  • StumbleUpon
  • TwitThis
Categories: ASP.NET

Getting the URI in Silverlight

by ashic 8. December 2008 09:52

We may need the uri of the silverlight app in various circumstances (like if we move the app around in the solution, or are dealing with images and other resources located in a folder on the web site that's not part of the silverlight project). We can get the path easily with:

App.Current.Host.Source

So, if we are to get a uri to an image in the Images folder in the web site that hosts the silverlight app, we can go:

var uri = new Uri(App.Current.Host.Source, "../Images/somepic.jpg");

Hope that helps.

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

Powered by BlogEngine.NET 1.6.1.0
Theme by Ashic Mahtab

Need an expert?

Ashic Mahtab
ashic@live.com
(+44) 07879927393

Stats

Featured Ads

 

Donations

I maintain this site and create all content entirely in my own time just to help you guys out. If you find the stuff helpful, or cool or just like what you see, I'd appreciate you chipping in to help out with the hosting costs. It's easy to do so - just click the button below - no amount is too low :)