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属性应用任何时间Select执行方法以检索数据。The 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绑定到的对象SelectedValue属性的DropDownList控件。In 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执行返回的对象Select方法。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. FilterParameters集合中包含的参数,其计算结果中找到占位符FilterExpression属性。The 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.

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

适用于

另请参阅