使用Compare Validator比较网格项模板中的两个下拉列表

时间:2013-12-11 13:08:31

标签: c# asp.net validation

我的网格中有两个下拉列表,如果选择了一个,而另一个未在客户端选择,则应填充错误消息,要求用户从第二个下拉列表中选择一个值。我想对这个功能使用比较验证器,任何帮助都深表赞赏

<Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:DropDownList ID="ddlrate1" runat="server" Width="300px">
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:DropDownList ID="ddlrate2" runat="server" Width="300px">
                                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
         <asp:TemplateField>
            <ItemTemplate>
                <asp:CompareValidator ID="CompareValidator1" runat="server" Operator = "LessThan" ErrorMessage="Rate1 should be less than Rate2" ControlToValidate = "ddlrate1" ControlToCompare = "ddlrate2"></asp:CompareValidator>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>

这是我正在尝试的请给我建议

1 个答案:

答案 0 :(得分:0)

您可以使用RequiredFieldValidator

<asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="Cause" InitialValue="0" runat="server"  ErrorMessage="Please Selecte One " Display="Dynamic">
</asp:RequiredFieldValidator>
<asp:DropDownList ID="Cause" runat="server" CssClass="drbox">
<asp:ListItem Value="0" Selected="True">- select one -</asp:ListItem>
<asp:ListItem Value="1" Selected="True">cause1</asp:ListItem>
</asp:DropDownList>