Isolate a required field validator?

2019-07-21 07:44发布

I have two search buttons on a page, one linked to a dropdown list and one linked to a dropdown list with a textbox for more search criteria. I have required field validators on all of the aforementioned controls. When I choose something from the first dropdown and click the appropriate search button, the field validator for the textbox fires, disabling the first search button. Is there a way to localize/isolate the validators to only associate with one of the two buttons? Code below:

     <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条回答
Deceive 欺骗
2楼-- · 2019-07-21 08:30

Yes, you can use the ValidationGroup property and set that validation group to your button control: 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" />
查看更多
登录 后发表回答