共用方式為


使用資料來源控制項篩選資料

更新:2007 年 11 月

資料來源控制項提供一些資料服務,讓您更容易在應用程式中加入進階功能。其中包含根據指定的搜尋準則篩選資料。篩選在使用快取資料時特別方便,因為可以在不需要重新執行查詢或呼叫方法讀取資料的情況下,提供搜尋功能。

若要篩選資料,資料來源控制項必須依照下列方式設定:

當使用 XmlDataSource 控制項時,您可以使用 XPath 查詢篩選資料。如需詳細資訊,請參閱使用 XmlDataSource 控制項篩選資料

設定篩選條件運算式

您可以設定資料來源物件的 FilterExpression 屬性,指定篩選條件套用至 ObjectDataSourceSqlDataSourceAccessDataSource 控制項傳回的資料。篩選條件運算式的語法,是根據 DataColumn 類別的 Expression 屬性語法。當呼叫資料來源控制項的 Select 方法時,會套用篩選條件運算式。

提供篩選條件參數

您可以提供 ObjectDataSourceSqlDataSourceAccessDataSource 控制項的參數型篩選條件運算式,這可以讓您不需要撰寫明確設定 FilterExpression 屬性的任何程式碼,就能在執行階段提供篩選條件值。您可以使用資料來源控制項的 FilterParameters 集合,指定篩選條件運算式參數。參數可以從控制項、QueryString 物件、工作階段狀態、使用者設定檔屬性等擷取資料。如需 FilterParameters 集合中能夠使用的參數型別資訊,請參閱使用含有資料來源控制項的參數

在篩選條件運算式中,建立對應至資料來源控制項之 FilterParameters 集合內項目的預留位置。預留位置是已編號的,0 代表集合中的第一個參數。您可以將篩選條件參數的編號放在 '{' 和 '}' 字元中,指定篩選條件運算式中的預留位置,如下列程式碼範例所示:

Country = '{0}' AND LastName LIKE '{1}'
安全性注意事項:

因為 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 >
    <title>Northwind Employees</title>
</head>
<body>
    <form id="form1" >

      <h3>Northwind Employees</h3>

        <table cellspacing="10">            
          <tr>
            <td valign="top">
              <table border="0">
                <tr>
                  <td valign="top">Country</td>
                  <td><asp:DropDownList  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  id="LastNameTextBox" Text="*" /></td>
                </tr>
                <tr>
                  <td></td>
                  <td><asp:Button  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 >
    <title>Northwind Employees</title>
</head>
<body>
    <form id="form1" >

      <h3>Northwind Employees</h3>

        <table cellspacing="10">            
          <tr>
            <td valign="top">
              <table border="0">
                <tr>
                  <td valign="top">Country</td>
                  <td><asp:DropDownList  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  id="LastNameTextBox" Text="*" /></td>
                </tr>
                <tr>
                  <td></td>
                  <td><asp:Button  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>

請參閱

其他資源

資料來源 Web 伺服器控制項