Pages

Saturday, April 26, 2008

CompareValidator Control

Sometimes you need to compare the value in one field to that of another field instead of to some preset value, range, or pattern. In these situations, you can use the CompareValidator control. Like other validation controls, this control uses a ControlToValidate property. In addition, it adds a ControlToCompare property. This property gives you the ability to specify another control against which the value of the control in the ControlToValidate property is compared.

If you want to compare the control's value against a constant value, you can use the ValueToCompare property instead of the ControlToCompare property. If you set both properties, the ControlToCompare property takes precedence over the ValueToCompare property.

The default comparison operator for the CompareValidator control is equality. However, this is programmable through the Operator property. Valid operators are defined by the ValidationCompareOperator enumeration and include Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, and DataTypeCheck. The DataTypeCheck operator is the only unary operator among them. It is used to determine whether the data in the control identified by the ControlToValidate property is of a certain type. In fact, if you set the ControlToCompare and ValueToCompare properties, they are ignored if the operator type is set to DataTypeCheck. The DataTypeCheck operator uses the value of the Type property as a base for determining the acceptable data type. The Type property is set based on the ValidationDataType enumeration as described in the RangeValidator control previously mentioned.

No comments: