共用方式為


SqlDataSource.FilterExpression 屬性

定義

取得或設定當呼叫 Select(DataSourceSelectArguments) 時會套用的篩選運算式。

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

屬性值

字串,代表當使用 Select(DataSourceSelectArguments) 方法抓取資料時套用的篩選運算式。

例外狀況

已設定 FilterExpression 屬性,且 SqlDataSource 處於 DataReader 模式中。

範例

下列程式代碼範例示範如何從 Northwind 資料庫擷取數據,並使用字串和FilterParameters集合加以篩選FilterExpressionFilterExpression每當方法執行以擷取數據時,Select就會套用 屬性。 在此範例中 FilterExpression ,包含篩選參數的佔位元,包含在集合中 FilterParameters 。 此外,filter 參數是 ControlParameter 係結至 SelectedValue 控件之 屬性的物件 DropDownListDropDownList因為控件的 AutoPostBack 屬性設定true為,所以控件選取範圍中的任何DropDownList變更都會讓頁面將資訊張貼回伺服器,以及GridView控件使用新的篩選重新系結至數據源控件。

<!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值。 篩選表達式語法是 屬性所RowFilter接受的相同語法,因為篩選表達式會套用至RowFilter從執行 Select 方法傳回之 對象的屬性DataView。 如需詳細資訊,請參閱Expression

如果您將參數新增至 FilterParameters 集合,您也可以在運算式中包含格式字串佔位元 (, "{0}") 以取代參數值。 佔位元會根據集合中 FilterParameters 參數的索引來取代。 如果集合中的 FilterParameters 物件為 null,則會以空字串取代物件。

您可以在 屬性中包含 FilterExpression 參數。 如果參數是字串或字元類型,請將參數括在單引號中。 如果參數是數值類型,則不需要引號。 集合 FilterParameters 包含針對屬性中找到 FilterExpression 的佔位元評估的參數。

只有在模式中DataSet時,控件SqlDataSource才支援篩選數據。

屬性FilterExpression會委派給FilterExpressionSqlDataSource 控件相關聯之 對象的屬性SqlDataSourceView

適用於

另請參閱