Programming Microsoft ASP.NET 4 - Dino Esposito [202]
Text
Gets or sets the description displayed for the validator in lieu of the error message. Note, though, this text does not replace the contents of ErrorMessage in the summary text.
ValidationGroup
Gets or sets the validation group that this control belongs to.
All validation controls inherit from the BaseValidator class except for compare validators, for which a further intermediate class—the BaseCompareValidator class—exists. The BaseCompareValidator class serves as the foundation for validators that perform typed comparisons. An ad hoc property, named Type, is used to specify the data type the values are converted to before being compared. The CanConvert static method determines whether the user’s entry can be converted to the specified data type. Supported types include string, integer, double, date, and currency. The classes acting as compare validators are RangeValidator and CompareValidator.
Note
You might want to pay careful attention when using the ForeColor property. Don’t get it wrong—there’s nothing bad with the property, which works as expected and sets the foreground color being used by the validators to show any messages. That’s just the point, however. Today’s applications tend to gain a lot more control over the style of emitted markup and for this reason tend to style through CSS wherever possible. Like many other similar style properties on server controls, the ForeColor property emits inline style information, which is really bad for designers when they get to do their job. Consider that in ASP.NET 4, validation controls no longer use the red color for error messages unless you set the ControlRenderingCompatabilityVersion attribute to “3.5” in the Associating Validators with Input Controls Not all server controls can be validated—only those that specify their validation property through an attribute named [ValidationProperty]. The attribute takes the name of the property that contains the user’s entry to check. For example, the validation property for a TextBox is Text and is indicated as follows: [ValidationProperty("Text")] public class TextBox : WebControl, ITextControl { ... } The list of controls that support validation includes TextBox, DropDownList, ListBox, RadioButtonList, FileUpload, plus a bunch of HTML controls such as HtmlInputFile, HtmlInputText, HtmlInputPassword, HtmlTextArea, and HtmlSelect. Custom controls can be validated too, as long as they are marked with the aforementioned [ValidationProperty] attribute. Note If the validation property of the associated input control is left empty, all validators accept any value and always pass the test. The RequiredFieldValidator control represents a rather natural exception to this rule, because it has been specifically designed to detect fields the user skipped and left blank. Gallery of Controls This said, let’s go ahead and take a closer look at the stock validation controls available in ASP.NET Web Forms. The CompareValidator Control
The link between each validator and its associated input control is established through the ControlToValidate property. The property must be set to the ID of the input control. If you do not specify a valid input control, an exception will be thrown when the page is rendered. The associated validator/control is between two controls within the same container—be it a page, user control, or template.
In general, ASP.NET validators are designed to work on a single control and process a single “value” for that control. As mentioned, you use the ValidationProperty attribute on custom controls to specify which property you want to validate. For stock controls, you take what they provide without many chances to modify things. Keep in mind that for validation scenarios that involve multiple controls or multiple properties, you need to create your own custom validation controls.
The CompareValidator