2013-08-23 32 views
-3

好吧,努力讓我的頭在下面的例子。 (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.filterexpression.aspx在數據網格上的多個文本框搜索

目標:有兩個下拉菜單/文本框用於編輯數據網格過濾。

我曾嘗試在=標題後添加FirstName ='{1}'「以及FirstName ='{0}'」,但這似乎不起作用。我爲FirstName添加了一個控制參數。

下面的代碼是我的版本,然後是原來的。

我的代碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
     <html xmlns="http://www.w3.org/1999/xhtml" > 
      <head runat="server"> 
      <title>ASP.NET Example</title> 
     </head> 
     <body> 
       <form id="form1" runat="server"> 

        <p>Show all employees with the following title: 
        <asp:DropDownList 
         id="DropDownList1" 
         runat="server" 
         AutoPostBack="True"> 
         <asp:ListItem Selected="True">A</asp:ListItem> 
         <asp:ListItem>B</asp:ListItem> 
         <asp:ListItem>C</asp:ListItem> 
        </asp:DropDownList></p> 

<asp:DropDownList 
         id="DropDownList2" 
         runat="server" 
         AutoPostBack="True"> 
         <asp:ListItem Selected="True">X</asp:ListItem> 
         <asp:ListItem>Y</asp:ListItem> 
         <asp:ListItem>Z</asp:ListItem> 
        </asp:DropDownList></p> 

        <asp:SqlDataSource 
         id="SqlDataSource1" 
         runat="server" 
         ConnectionString="<%$ ConnectionStrings:MyNorthwind%>" 
         SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees" 
         FilterExpression="Title='{0}' or "FirstName='{1}'"> 
         <FilterParameters> 
          <asp:ControlParameter Name="Title" ControlId="DropDownList1" PropertyName="SelectedValue"/> 
<asp:ControlParameter Name="FirstName" ControlId="DropDownList1" PropertyName="SelectedValue"/> 
         </FilterParameters> 
        </asp:SqlDataSource> 

        <p><asp:GridView 
         id="GridView1" 
         runat="server" 
         DataSourceID="SqlDataSource1" 
         AutoGenerateColumns="False"> 
         <columns> 
          <asp:BoundField Visible="False" DataField="EmployeeID" /> 
          <asp:BoundField HeaderText="First Name" DataField="FirstName" /> 
          <asp:BoundField HeaderText="Last Name" DataField="LastName" /> 
         </columns> 
        </asp:GridView></p> 

       </form> 
      </body> 
     </html> 
+0

正如我所說的,我sturggling左右讓我的頭,並張貼只有原來的代碼,因爲我覺得它比我玩過的(並且可能有誤導性的)代碼更好。無論如何,幫助表示讚賞。 – indofraiser

回答

0

我本來有一個逗號,分離該搜索查詢。我其實需要把'或'。

查看原帖(編輯是正確的),並在下面的示例代碼完整的代碼..

<asp:SqlDataSource 
         id="SqlDataSource1" 
         runat="server" 
         ConnectionString="<%$ ConnectionStrings:MyNorthwind%>" 
         SelectCommand="SELECT EmployeeID,FirstName,LastName,Title FROM Employees" 
         FilterExpression="Title='{0}' or "FirstName='{1}'">