System.Web.UI.WebControls Namespace
Compares the value entered by the user into an input control with the value entered into another input control or a constant value.
Use the CompareValidator control to compare the value entered by the user into an input control, such as a TextBox control, with the value entered into another input control or a constant value. You can also use the CompareValidator control to indicate whether the value entered into an input control can be converted to the data type specified by the Type property.
Specify the input control to validate by setting the ControlToValidate property. If you need to compare a specific input control with another input control, set the ControlToCompare property to specify the control to compare with.
NOTE: If the value entered into the input control specified by the ControlToCompare property cannot be converted to the data type specified by the Type property, but the value entered into the input control specified by the ControlToValidate property can be converted, the input control being validated is considered valid. Therefore, you should also place a CompareValidator or RangeValidator control on the input control specified by the ControlToCompare property.
Instead of comparing the value of an input control with another input control, you can compare the value of an input control to a constant value. Specify the constant value to compare with by setting the ValueToCompare property.
NOTE: If the value specified by the ValueToCompare property cannot be converted to the data type specified by the Type property, an exception is thrown. Be sure to check the data type of a value before programmatically assigning it to the ValueToCompare property.
You can either compare the value of an input control to another input control using ControlToCompare, or to a constant value using ValueToCompare. If both properties are set, the ControlToCompare property takes precedence.
Use the Operator property to specify the type of comparison to perform, such as greater than, equal to, and so on. If you set the Operator property to DataTypeCheck, the CompareValidator control ignores the ControlToCompare and ValueToCompare properties and simply indicates whether the value entered into the input control can be converted to the data type specified by the Type property.The Type property is used to specify the data type of both comparison values. Both values are automatically converted to this data type before the comparison operation is performed.
NOTE: Validation succeeds if the input control is empty. To force the user to enter data into the input control, use a RequiredFieldValidator.
For examples illustrating use of this control, see the individual member types of this class. For syntax information, see CompareValidator in ASP.NET Syntax for Validation Controls.
Validating Against a Specific Value Validating Against a Data Type Validating Against Values in a Database