SqlDataSourceView.FilterExpression SqlDataSourceView.FilterExpression SqlDataSourceView.FilterExpression SqlDataSourceView.FilterExpression Property

定义

获取或设置调用 Select 方法时应用的筛选表达式。Gets or sets a filtering expression that is applied when the Select 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.

异常

示例

下面的代码示例演示如何使用FilterExpressionFilterParameters属性从 Northwind 数据库中检索数据并对其进行筛选。The following code example demonstrates how to retrieve data from the Northwind database and filter it using the FilterExpression and FilterParameters properties. 只要FilterExpression执行Select方法来SqlDataSource检索数据, 就会应用控件的属性。The FilterExpression property of the SqlDataSource control is applied any time the Select method is executed to retrieve data. 在此示例中, FilterExpression属性包含一个筛选器参数的占位符, 该参数包含FilterParameters在集合中。In this example, the FilterExpression property contains a placeholder for a filter parameter, which is contained in the FilterParameters collection. 在此示例中, filter 参数是一个ControlParameter对象, 该对象绑定SelectedValueDropDownList控件的属性。In this example, the filter parameter is a ControlParameter object that is bound to the SelectedValue property of the DropDownList control. AutoPostBack true DropDownList由于控件将其属性设置为, 因此所GridView选内容中的任何更改都会导致页面向服务器发送内容, 并将控件重新绑定到带有新筛选器的数据源控件。 DropDownListBecause the DropDownList control has its AutoPostBack property set to true, any change in the DropDownList selection causes the page to post 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属性的语法是格式字符串样式的语法。The syntax that is used for the FilterExpression property is a format string-style syntax. 可以在FilterExpression属性中包含参数。You can include parameters in the FilterExpression property. 如果参数的类型为 "字符串" 或 "字符", 则将参数用单引号引起来。If the type of the parameter is string or character, 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.

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

FilterExpression属性的值存储在视图状态中。The value of the FilterExpression property is stored in view state.

重要

应该验证从客户端接收的任何筛选器参数值。You should validate any filter parameter value that you receive from the client. 运行时只需将参数值替换为筛选表达式, 并将其DataView应用于Select方法返回的对象。The runtime simply substitutes the parameter value into the filter expression and applies it to the DataView object that is returned by the Select method. 如果使用FilterExpression属性作为安全措施来限制返回的项目数, 则必须在进行筛选之前验证参数值。If you are using the FilterExpression property as a security measure to limit the number of items that are returned, you must validate the parameter values before the filtering occurs.

适用于

另请参阅