隔离必填字段验证器?

时间:2011-05-20 09:43:52

标签: asp.net html visual-studio

我在页面上有两个搜索按钮,一个链接到下拉列表,另一个链接到带有文本框的下拉列表以获取更多搜索条件。我在所有上述控件上都需要字段验证器。当我从第一个下拉列表中选择内容并单击相应的搜索按钮时,文本框的字段验证器将触发,禁用第一个搜索按钮。有没有办法将验证器本地化/隔离,只与两个按钮之一相关联?代码如下:

     <asp:Panel ID="Panel1" runat="server" HorizontalAlign="Center">
<asp:Label ID="Label1" runat="server" Text="Search by status:"></asp:Label>
        &nbsp;
        <asp:DropDownList ID="DdlStatus" runat="server" 
            DataSourceID="SqlDataSource2" DataTextField="Status" DataValueField="Status" AppendDataBoundItems="true">
            <asp:ListItem Text="Choose a status" Value="0" Selected="True"></asp:ListItem>
        </asp:DropDownList>
        &nbsp;
        <asp:Button ID="BtnStatusSearch" runat="server" Text="Search" onclick="BtnStatusSearch_Click" />
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" 
            SelectCommand="SELECT [Status] FROM [Status]"></asp:SqlDataSource>
        <asp:Label ID="LblSearch" runat="server" Text="Other search:"></asp:Label>
        &nbsp;
        <asp:DropDownList ID="DdlSearch" runat="server">
            <asp:ListItem Selected="True" Value="0">Choose search criteria</asp:ListItem>
            <asp:ListItem Value="1">Broker</asp:ListItem>
            <asp:ListItem Value="2">Customer</asp:ListItem>
            <asp:ListItem Value="3">Customer State</asp:ListItem>
            <asp:ListItem Value="4">Broker State</asp:ListItem>
        </asp:DropDownList><asp:RequiredFieldValidator ID="RfvDdlSearch" runat="server" Display="Dynamic" 
            ErrorMessage="Required field" ControlToValidate="DdlSearch" CssClass="ErrorMessage"></asp:RequiredFieldValidator>
        &nbsp;&nbsp;
        <asp:TextBox ID="TbSearch" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfvTbSearch" runat="server" Display="Dynamic" 
            ErrorMessage="Required field" ControlToValidate="TbSearch" CssClass="ErrorMessage"></asp:RequiredFieldValidator>
&nbsp;&nbsp;
        <asp:Button ID="BtnSearch" runat="server" onclick="BtnSearch_Click" Text="Search" />

1 个答案:

答案 0 :(得分:4)

是的,您可以使用ValidationGroup属性并将该验证组设置为按钮控件:ValidationGroup="button1"

 <asp:RequiredFieldValidator ID="rfv" runat="server" ValidationGroup="button1"
        ErrorMessage="*"></asp:RequiredFieldValidator>
  <asp:Button ID="btnLogin" runat="server" Text="Login" ValidationGroup="button1" OnClick="btnLogin_Click" />