Coded – Web Development and Programming Blog

C#, ASP.NET, Google, Remoting, AJAX, Silverlight, Web Development

Automatically adding ValidatorCalloutExtenders to your validators

Posted in .NET Framework, ASP.NET, C#, Web Programming by Andrei Alecu on the July 16th, 2008

I recently had to work on a pretty big ASP.NET page with lots of fields that needed to be validated.

requiredfieldvalidator.png We thought it would be cool if we used the AJAX Toolkit ValidatorCalloutExtender control on the validators to keep the validation inline and concise.

To quote from the AJAX Toolkit page:

ValidatorCallout is an ASP.NET AJAX extender that enhances the functionality of existing ASP.NET validators. To use this control, add an input field and a validator control as you normally would. Then add the ValidatorCallout and set its TargetControlID property to reference the validator control.

Because we had over 30 text fields, it would have been really tiresome to add extenders manually to each of the validators. So a way to attach them dynamically was needed.

Fortunately, this is pretty easy to do by iterating through the Page.Validators collection, dynamically creating ValidatorCalloutExtender controls and adding them to the Page.

Challenges

  • The first problem I had was an error that occured when trying to dynamically add controls to the Page.Controls collection:
(more…) Bookmark on del.icio.us