How to use compare validator to compare the data between two dropdownlists values?

只愿长相守 提交于 2019-12-24 00:37:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!