SqlDataSource.FilterExpression 屬性

定義

取得或設定當呼叫 Select(DataSourceSelectArguments) 時會套用的篩選運算式。Gets or sets a filtering expression that is applied when the Select(DataSourceSelectArguments) method is called.

public:
 property System::String ^ FilterExpression { System::String ^ get(); void set(System::String ^ value); };
public string FilterExpression { get; set; }
member this.FilterExpression : string with get, set
Public Property FilterExpression As String

屬性值

String

字串,代表當使用 Select(DataSourceSelectArguments) 方法抓取資料時套用的篩選運算式。A string that represents a filtering expression applied when data is retrieved using the Select(DataSourceSelectArguments) method.

例外狀況

已設定 FilterExpression 屬性,且 SqlDataSource 處於 DataReader 模式中。The FilterExpression property was set and the SqlDataSource is in DataReader mode.

範例

下列程式碼範例示範如何從 Northwind 資料庫取出資料,並使用 FilterExpression 字串和集合進行篩選 FilterParametersThe following code example demonstrates how to retrieve data from the Northwind database and filter it using a FilterExpression string and the FilterParameters collection. FilterExpression每當 Select 執行方法來取得資料時,就會套用此屬性。The FilterExpression property is applied any time the Select method is executed to retrieve data. 在此範例中, FilterExpression 會包含包含在集合中之篩選參數的預留位置 FilterParametersIn this example, the FilterExpression contains a placeholder for a filter parameter, which is contained in the FilterParameters collection. 此外,篩選參數是系結 ControlParameterSelectedValue 控制項屬性的物件 DropDownListIn addition, the filter parameter is a ControlParameter object that is bound to the SelectedValue property of the DropDownList control. 由於 DropDownList 控制項的 AutoPostBack 屬性設定為,因此對 true 控制項所做的任何變更 DropDownList 都會導致頁面將資訊回傳至伺服器,而控制項則會 GridView 以新的篩選器重新系結至資料來源控制項。Because the DropDownList control has its AutoPostBack property set to true, any change in the selection for the DropDownList control causes the page to post information back to the server and the GridView control to rebind to the data source control with the new filter.

<!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">Sales Representative</asp:ListItem>
                <asp:ListItem>Sales Manager</asp:ListItem>
                <asp:ListItem>Vice President, Sales</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}'">
                <FilterParameters>
                    <asp:ControlParameter Name="Title" 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>
<!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">Sales Representative</asp:ListItem>
                <asp:ListItem>Sales Manager</asp:ListItem>
                <asp:ListItem>Vice President, Sales</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}'">
                <FilterParameters>
                    <asp:ControlParameter Name="Title" 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>

備註

FilterExpression屬性值是格式字串運算式 (由方法所處理的字串, String.Format) 使用 FilterExpression 集合中包含在字串中的任何替代參數的值。The FilterExpression property value is a format string expression (a string that is processed by the String.Format method) that uses the values in the FilterExpression collection for any substitution parameters included in the string. 篩選運算式語法與屬性所接受的語法相同 RowFilter ,因為篩選運算式會套用至 RowFilter DataView 執行方法所傳回之物件的屬性 SelectThe filter expression syntax is the same syntax that is accepted by the RowFilter property, because the filter expression is applied to the RowFilter property of the DataView object that is returned from executing the Select method. 如需詳細資訊,請參閱ExpressionFor more information, see Expression.

如果您將參數加入至 FilterParameters 集合,您也可以在運算式中包含格式字串預留位置 (例如, "{0}") 用來取代參數值。If you add parameters to the FilterParameters collection, you can also include format string placeholders (for example, "{0}") in the expression to substitute for parameter values. 系統會根據集合中的參數索引來取代預留位置 FilterParametersThe placeholders are replaced according to the index of the parameter in the FilterParameters collection. 如果集合中的物件 FilterParametersnull ,物件將會取代為空字串。If an object in the FilterParameters collection is null, the object will be replaced by an empty string.

您可以在屬性中包含參數 FilterExpressionYou can include parameters in the FilterExpression property. 如果參數是字串或字元類型,請使用單引號括住參數。If the parameter is a string or character type, enclose the parameter in single quotation marks. 如果參數為數數值型別,則不需要引號。Quotation marks are not required, if the parameter is a numeric type. FilterParameters集合包含的參數會針對在屬性中找到的預留位置進行評估 FilterExpressionThe FilterParameters collection contains the parameters that are evaluated for the placeholders that are found in the FilterExpression property.

SqlDataSource只有在模式中時,控制項才支援篩選資料 DataSetThe SqlDataSource control supports filtering data only when in the DataSet mode.

FilterExpression屬性會委派給 FilterExpression SqlDataSourceView 與控制項相關聯之物件的屬性 SqlDataSourceThe FilterExpression property delegates to the FilterExpression property of the SqlDataSourceView object that is associated with the SqlDataSource control.

適用於

另請參閱