问题
I have two DropDownLists
populated with Year-Dates
, and I want to show an error message in the case where the second ddls value is less than the first ddls value.
This is the code I have used so far, and it doesn't work:
<asp:CompareValidator
ID="cvEndYear2" Operator="GreaterThan" runat="server" CssClass="text-danger"
ValidationGroup="Save" ControlToValidate="ddlEndYear" Display="Dynamic"
ValueToCompare="ddlStartYear" ErrorMessage="Greater Than" SetFocusOnError="true">
</asp:CompareValidator>
回答1:
You have to specify the ControlToCompare
and the Operator
:
<asp:CompareValidator
ID="cvEndYear2" Operator="GreaterThan" runat="server" CssClass="text-danger"
ValidationGroup="Save"
ControlToValidate="ddlEndYear" Display="Dynamic"
ControlToCompare="ddlStartYear"
Operator="GreaterThanEqual"
Type="Integer"
ErrorMessage="The end year must be greater/equal the start year" SetFocusOnError="true">
</asp:CompareValidator>
回答2:
You haven't specified the type, specify the type of data which would be in the textboxes :
<asp:CompareValidator
..........
..........
ControlToValidate="ddlStartYear"
ControlToCompare="ddlEndYear"
Operator="GreaterThanEqual"
Type="Integer">
</asp:CompareValidator>
You might want to have a look at this tutorial
来源:https://stackoverflow.com/questions/40997560/how-to-use-compare-validator-to-compare-the-data-between-two-dropdownlists-value