如何防止具有相同值集的下拉列表选择相同的值?

时间:2014-03-12 10:58:48

标签: asp.net dropdownlistfor

我有3个下拉列表,每个下拉列表中都有相同的值,例如a,b和c

例如:如果我从下拉列表中选择“a”,并且选择了相同值“a”的另一个下拉列表。我应该得到一个错误的消息。

这怎么可能。

1 个答案:

答案 0 :(得分:0)

像这样使用CompareValidator控件:

<form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="ddl1" runat="server">
            <asp:ListItem Text="a" Value="a"></asp:ListItem>
            <asp:ListItem Text="b" Value="b"></asp:ListItem>
        </asp:DropDownList>
        <asp:DropDownList ID="ddl2" runat="server">
            <asp:ListItem Text="a" Value="a"></asp:ListItem>
            <asp:ListItem Text="b" Value="b"></asp:ListItem>
        </asp:DropDownList>
        <asp:DropDownList ID="ddl3" runat="server">
            <asp:ListItem Text="a" Value="a"></asp:ListItem>
            <asp:ListItem Text="b" Value="b"></asp:ListItem>
        </asp:DropDownList>
        <asp:CompareValidator ID="CompareValidator1" runat="server"
            ErrorMessage="DropDownList 1 can't be equal DropDownList 2"
            ControlToCompare="ddl1" ControlToValidate="ddl2" Operator="NotEqual"></asp:CompareValidator>
        <asp:CompareValidator ID="CompareValidator2" runat="server"
            ErrorMessage="DropDownList 2 can't be equal DropDownList 3"
            ControlToCompare="ddl2" ControlToValidate="ddl3" Operator="NotEqual"></asp:CompareValidator>
         <asp:CompareValidator ID="CompareValidator3" runat="server"
            ErrorMessage="DropDownList 1 can't be equal DropDownList 3"
            ControlToCompare="ddl2" ControlToValidate="ddl3" Operator="NotEqual"></asp:CompareValidator>
    </div>
</form>