2011-05-20 140 views
2

我在頁面上有兩個搜索按鈕,一個鏈接到下拉列表,另一個鏈接到帶有文本框的下拉列表以獲取更多搜索條件。我已經要求對上述所有控件進行字段驗證。當我從第一個下拉列表中選擇一些內容並單擊適當的搜索按鈕時,文本框的字段驗證程序會觸發,禁用第一個搜索按鈕。有沒有辦法將驗證器本地化/隔離,只與兩個按鈕中的一個相關聯?下面的代碼:隔離必填字段驗證程序?

 <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" /> 

回答

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" /> 
+0

你真棒。 – 2011-05-20 09:54:50

+0

非常感謝...它幫助了我太多 – Dini88 2013-09-11 07:10:45