Ever find yourself creating an input form in ASP.NET that required a lot of Controls to be validated?
Well, I have. It's tedious. You have to add at least one validator to each Control you wanted validated. Apparently after discussing this with other people I've found that I'm not the only person who was wishing at the time that they could just add one Control that would validate all the Controls on the Page. What's the solution? To create a Control on your own to do just that and that's what I did.
Here's how you use it:
You add the Control to your page (sorry no “drag and drop” stuff I don't believe in drag and drop so I didn't find the need to write that kind of code but be my guest). You set my Control's ControlToValidate property to a server Control containing all Controls you want to validate, eg. the DIV containing your let's say TextBox Controls. That's it.
Magic huh? Not really, here's how it works:
My Control gets the ControlToValidate's Controls collection. It goes through the Controls collection and checks for controls that can be validated [0] and adds a validator control [1] to the ControlToValidate's Controls collection [2] immediately following any control that can be validated. [0] To check for Controls that can be validated it uses the BaseValidator's GetValidationProperty method. Now my control doesn't inherit from BaseValidator so I have an internal class that does and exposes a function the just returns the value of BaseValidator.GetValidationProperty(). [1] The type of validator that is added is determined by you. I really wrote an abstract class that you must inherit from and implement one method that returns a validator. I've written the first one for you so you have an example. It adds RequiredFieldValidators. [2] Controls don't have access to manipulate each other's Controls collection so I had to kind of hack my way around this. I used reflection to change a private field in the ControlToValidate so that it would allow me to modify its collection. Don't worry I change it back when I'm done :).
How do you get a hold of my Control:
You download it from my perpetually under construction homepage. The code isn't elegant or well documented nor does it provide you with all the other nice things Andy Smith's MetaBuilders website Controls provide you with but it works. Feel free to modify it as you will. If you feel like you've modified it and created something of value all I ask is that you share it.
The one caveat is that my Control has to be before the ControlToValidate in the Page (or user control, custom control, composite control, etc)'s Controls collection. The performance hit for using reflection was about 5% from my calculations on a Page with three Controls to validate however my testing environment was bad so I decided to quit testing. I really don't think it will really affect your performance that much though. Feel free to test it yourself and let me know the results.
One last thing just in case USE AT YOUR OWN RISK. Oh and I guess people always say don't redistribute so don't do that either (I don't quite know why but still).