日期:2009-07-29 浏览次数:20433 次
The article explains how to make any Control follow standard validation schema in ASP.NET with very few lines of code.
After going through Microsoft’s quick-start tutorial and samples it seemed like the burden of user input validation was finally taken off shoulders of poor developers with a reach palette of validation controls. I can’t speak for every ASP developer, but I personally, as well as people in my team spend at least 30% of their development time doing just that - creating different validation mechanisms and making sure that they work for all popular browsers. Excited about the idea of saving development time I started to use Validation controls everywhere only to find out that some web controls can’t be hooked up for validation.
In my particular case, I needed to make sure that a user has checked at least one check box in a CheckBoxList control. Having previous experience with RadioButtonList control, I just set the ControlToValidate property of a RequiredFieldValidator control to an ID of a CheckBoxList control and expected it to work seamlessly. It did work for a RadioButtonList control, but as soon as I set ControlToValidate property to a CheckBoxList control I was getting an error: “Control 'question4' referenced by the ControlToValidate property of 'validator1' cannot be validated”. That is when the exploration began.
Apparently, not all of the web controls can be validated. In retrospective, it does make sense, because some controls can have no way of “knowing” what exactly do you want to validate about them. However at the beginning I was surprised to see that validation works for one and doesn’t work for the other of the two controls types that are very similar in behavior (RadioButtonList CheckBoxList). I tried just about everything, from creating a CustomValidator control to inheriting from the RequiredFieldValidator control and overriding some of its methods – nothing worked until somebody posted a massage on an ASPngbeta list (which I highly recommend - ASPngbeta@ls.ASPlists.com). The message was explaining a completely different issue but it mentioned that you could pretty much see a source code of any .NET libraries using an ildasm utility. Well, it is not exactly a C# or a VB source code but it is much more readable than any kind of assembler – you can almost write such code by hand. Using the tool I found out that all validation controls are looking for an attribute called “ValidationProperty”.
The rest was just a piece of cake: All I needed to do was to derive a class (MyCheckBoxList) from a CheckBoxList and identify a validation property – see code insert below. I guess I could use just any property that generates some value when any check box is checked within the control, but I created my own, because I thought that an extra property might be useful beyond validation issues. That took care of compilation errors and server side was “happy”.
namespace Project1.control
{
using System;
using System.Web.UI;