Example 15 CompareValidator.aspx |
<Script Runat="Server">
Sub Button_Click (s as Object, e As EventArgs)
If IsValid Then
Response.Redirect (“Thanks.aspx" )
End If
End Sub
</Script>
<html>
<head><title>CompareValidator.aspx</title></head>
<body>
<form Runat="Server">
Start Date:
<asp:TextBox
id="txtStartDate"
Columns="8"
Runat="Server"/>
End Date:
<asp:TextBox
id="txtEndDate"
Columns="8"
Runat="Server"/>
<br>
<asp:CompareValidator
ControlToValidate="txtEndDate"
ControlToCompare="txtStartDate"
Display="Dynamic"
Text="Start date must be smaller than end date!"
Operator="GreaterThan"
Type="Date"
Runat="Server" />
<p>
<asp:Button
Text="Submit!"
OnClick="Button_Click"
Runat="Server"/>
</form>
</body>
</html> |
| |
The output of this program is shown below:
In the above example the CompareValidator control uses the ControlToValidate and ControlToCompare properties to indicate the controls to be used for the comparison.
The Operator property has the value GreaterThan. CompareValidator checks whether the value entered into the txtEndDate control is greater than the value entered into the txtStartDate control.
Finally, the Type property has the value Date. The control compares the two values as date values rather than string or integer values.