SqlDataSource.FilterExpression SqlDataSource.FilterExpression SqlDataSource.FilterExpression SqlDataSource.FilterExpression Property

定义

获取或设置调用 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

属性值

表示使用 Select(DataSourceSelectArguments) 方法检索数据时应用的筛选表达式的字符串。A string that represents a filtering expression applied when data is retrieved using the Select(DataSourceSelectArguments) method.

异常

示例

下面的代码示例演示如何从 Northwind 数据库检索数据, 并使用FilterExpression字符串和集合对FilterParameters其进行筛选。The following code example demonstrates how to retrieve data from the Northwind database and filter it using a FilterExpression string and the FilterParameters collection. 只要执行方法来检索数据, 就会应用属性。FilterExpression SelectThe FilterExpression property is applied any time the Select method is executed to retrieve data. 在此示例中, FilterExpression包含一个筛选器参数的占位符, 该参数包含FilterParameters在集合中。In this example, the FilterExpression contains a placeholder for a filter parameter, which is contained in the FilterParameters collection. 此外, 筛选器参数是一个ControlParameter对象, 该对象绑定SelectedValueDropDownList控件的属性。In addition, the filter parameter is a ControlParameter object that is bound to the SelectedValue property of the DropDownList control. AutoPostBack true DropDownList由于控件将其属性设置为, 因此, 对控件所做的任何更改都将导致该页将信息发布回服务器, 并将GridView控件重新绑定到数据源DropDownList用新筛选器进行控制。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>

注解

属性值是格式字符串表达式 ( String.Format由方法处理的字符串), 该表达式FilterExpression使用集合中包含在字符串中的任何替换参数的值。 FilterExpressionThe 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于从执行Select方法返回的DataView对象的属性。The 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. 根据FilterParameters集合中参数的索引替换占位符。The placeholders are replaced according to the index of the parameter in the FilterParameters collection. 如果FilterParameters集合中的某个对象为null, 则该对象将替换为空字符串。If an object in the FilterParameters collection is null, the object will be replaced by an empty string.

可以在FilterExpression属性中包含参数。You 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. 集合包含为FilterExpression属性中的占位符计算的参数。 FilterParametersThe FilterParameters collection contains the parameters that are evaluated for the placeholders that are found in the FilterExpression property.

SqlDataSource 只有DataSet在模式下, 控件才支持筛选数据。The SqlDataSource control supports filtering data only when in the DataSet mode.

属性委托SqlDataSourceView FilterExpression给与SqlDataSource控件相关联的对象的属性。 FilterExpressionThe FilterExpression property delegates to the FilterExpression property of the SqlDataSourceView object that is associated with the SqlDataSource control.

适用于

另请参阅