更新:2007 年 11 月
数据源控件提供多种数据服务,这样将高级功能添加到应用程序会变得更加容易。这包括根据指定的搜索条件筛选数据。筛选对于使用缓存数据尤其方便,因为您无需重新运行查询或调用读取数据的方法就可以提供搜索功能。
若要筛选数据,必须按下列方式配置数据源控件:
- 对于 SqlDataSource 或 AccessDataSource 控件,必须将 DataSourceMode 属性设置为 DataSet。 
- 对于 ObjectDataSource 控件,基础源对象必须返回 DataSet 或 DataTable 对象。 
使用 XmlDataSource 控件时,可以使用 XPath 查询筛选数据。有关更多信息,请参见使用 XmlDataSource 控件筛选数据。
设置筛选器表达式
通过设置数据源控件的 FilterExpression 属性,指定要应用到 ObjectDataSource、SqlDataSource 或 AccessDataSource 控件所返回的数据的筛选器。筛选器表达式的语法基于 DataColumn 类的 Expression 属性的语法。当调用数据源控件的 Select 方法时,将应用筛选器表达式。
提供筛选器参数
您可以为 ObjectDataSource、SqlDataSource 或 AccessDataSource 控件提供参数化的筛选器表达式,这使您可以在运行时提供筛选值,而无需编写任何代码来显式设置 FilterExpression 属性。使用数据源控件的 FilterParameters 集合指定筛选器表达式参数。参数可以检索控件中的数据、QueryString 对象、会话状态、用户配置文件属性等等。有关可以在 FilterParameters 集合中使用的参数类型的信息,请参见对数据源控件使用参数。
在筛选器表达式中,创建与数据源控件的 FilterParameters 集合中的项对应的占位符。占位符是带编号的,0 表示集合中的第一个参数。通过将筛选器参数的编号放入“{”和“}”字符中间,可以指定筛选器表达式的占位符,如以下示例所示:
Country = '{0}' AND LastName LIKE '{1}'
| .gif) 安全说明: | 
|---|
| 因为 FilterParameters 集合中的值被替换为没有编码的 FilterExpression 字符串,所以在应用筛选器之前应当验证所有筛选器参数的值。在应用筛选器之前,可以使用数据源控件的 Filtering 事件访问并验证筛选器参数的值。 | 
下面的示例演示一个包含筛选器参数、名为 EmployeeDetailsSqlDataSource 的 SqlDataSource 控件。FilterExpression 属性中使用的参数值在运行时从页上其他位置的控件的属性值进行填充。
<%@ Page language="VB" %>
<!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>Northwind Employees</title>
</head>
<body>
    <form id="form1" runat="server">
      <h3>Northwind Employees</h3>
        <table cellspacing="10">            
          <tr>
            <td valign="top">
              <table border="0">
                <tr>
                  <td valign="top">Country</td>
                  <td><asp:DropDownList runat="server" id="CountryListBox" AppendDataBoundItems="True"
                                        DataSourceID="CountrySqlDataSource" 
                                        DataTextField="Country" DataValueField="Country" >
                        <asp:ListItem Selected="True" Value="" >(Show All)</asp:ListItem>
                      </asp:DropDownList>
                  </td>
                </tr>
                <tr>
                  <td>Last Name</td>
                  <td><asp:TextBox runat="server" id="LastNameTextBox" Text="*" /></td>
                </tr>
                <tr>
                  <td></td>
                  <td><asp:Button runat="server" id="FilterButton" Text="Filter Results" /></td>
                </tr>
              </table>
            </td>
            <td valign="top">                
              <asp:GridView ID="EmployeesGridView"
                DataSourceID="EmployeeDetailsSqlDataSource"
                AutoGenerateColumns="false"
                AllowSorting="True"
                DataKeyNames="EmployeeID"     
                Gridlines="Both"
                RunAt="server">
                <HeaderStyle backcolor="Navy"
                  forecolor="White"/>
                <RowStyle backcolor="White"/>
                <AlternatingRowStyle backcolor="LightGray"/>
                <EditRowStyle backcolor="LightCyan"/>
                <Columns>                  
                  <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" ReadOnly="true"/>                    
                  <asp:BoundField DataField="FirstName"  HeaderText="First Name"/>
                  <asp:BoundField DataField="LastName"   HeaderText="Last Name"/>                    
                  <asp:BoundField DataField="Country"    HeaderText="Country"/>                    
                </Columns>                 
              </asp:GridView>
            </td>                
          </tr>            
        </table>
        <asp:SqlDataSource ID="CountrySqlDataSource" 
          SelectCommand="SELECT DISTINCT Country FROM Employees"
          EnableCaching="True"
          CacheDuration="60"
          ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>"
          RunAt="server" />
        <asp:SqlDataSource ID="EmployeeDetailsSqlDataSource" 
          SelectCommand="SELECT EmployeeID, LastName, FirstName, Country FROM Employees"
          EnableCaching="True"
          CacheDuration="60"
          ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>"
          FilterExpression="Country LIKE '{0}' AND LastName LIKE '{1}'"
          RunAt="server">
          <FilterParameters>
            <asp:ControlParameter ControlID="CountryListBox"   PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="LastNameTextBox" PropertyName="Text" />
          </FilterParameters>
        </asp:SqlDataSource>
      </form>
  </body>
</html>
<%@ Page language="C#" %>
<!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>Northwind Employees</title>
</head>
<body>
    <form id="form1" runat="server">
      <h3>Northwind Employees</h3>
        <table cellspacing="10">            
          <tr>
            <td valign="top">
              <table border="0">
                <tr>
                  <td valign="top">Country</td>
                  <td><asp:DropDownList runat="server" id="CountryListBox" AppendDataBoundItems="True"
                                        DataSourceID="CountrySqlDataSource" 
                                        DataTextField="Country" DataValueField="Country" >
                        <asp:ListItem Selected="True" Value="" >(Show All)</asp:ListItem>
                      </asp:DropDownList>
                  </td>
                </tr>
                <tr>
                  <td>Last Name</td>
                  <td><asp:TextBox runat="server" id="LastNameTextBox" Text="*" /></td>
                </tr>
                <tr>
                  <td></td>
                  <td><asp:Button runat="server" id="FilterButton" Text="Filter Results" /></td>
                </tr>
              </table>
            </td>
            <td valign="top">                
              <asp:GridView ID="EmployeesGridView"
                DataSourceID="EmployeeDetailsSqlDataSource"
                AutoGenerateColumns="false"
                AllowSorting="true"
                DataKeyNames="EmployeeID"     
                Gridlines="Both"
                RunAt="server">
                <HeaderStyle backcolor="Navy"
                  forecolor="White"/>
                <RowStyle backcolor="White"/>
                <AlternatingRowStyle backcolor="LightGray"/>
                <EditRowStyle backcolor="LightCyan"/>
                <Columns>                  
                  <asp:BoundField DataField="EmployeeID" HeaderText="Employee ID" ReadOnly="true"/>                    
                  <asp:BoundField DataField="FirstName"  HeaderText="First Name"/>
                  <asp:BoundField DataField="LastName"   HeaderText="Last Name"/>                    
                  <asp:BoundField DataField="Country"    HeaderText="Country"/>                    
                </Columns>                 
              </asp:GridView>
            </td>                
          </tr>            
        </table>
        <asp:SqlDataSource ID="CountrySqlDataSource" 
          SelectCommand="SELECT DISTINCT Country FROM Employees"
          EnableCaching="True"
          CacheDuration="60"
          ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>"
          RunAt="server" />
        <asp:SqlDataSource ID="EmployeeDetailsSqlDataSource" 
          SelectCommand="SELECT EmployeeID, LastName, FirstName, Country FROM Employees"
          EnableCaching="True"
          CacheDuration="60"
          ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>"
          FilterExpression="Country LIKE '{0}' AND LastName LIKE '{1}'"
          RunAt="server">
          <FilterParameters>
            <asp:ControlParameter ControlID="CountryListBox"   PropertyName="SelectedValue" />
            <asp:ControlParameter ControlID="LastNameTextBox" PropertyName="Text" />
          </FilterParameters>
        </asp:SqlDataSource>
      </form>
  </body>
</html>