SqlDataSource.FilterExpression Property

定义

获取或设置调用 Select(DataSourceSelectArguments) 方法时应用的筛选表达式。

C#
public string FilterExpression { get; set; }

属性值

表示使用 Select(DataSourceSelectArguments) 方法检索数据时应用的筛选表达式的字符串。

异常

已设置 FilterExpression 属性,且 SqlDataSource 处于 DataReader 模式。

示例

下面的代码示例演示如何从 Northwind 数据库中检索数据和筛选使用其FilterExpression字符串和FilterParameters集合。FilterExpression属性应用任何时间Select执行方法以检索数据。 在此示例中,FilterExpression包含一个筛选器参数,它包含在一个占位符FilterParameters集合。 此外,筛选器参数是ControlParameter绑定到的对象SelectedValue属性的DropDownList控件。 因为DropDownList控件具有其AutoPostBack属性设置为true,为所选内容中的任何更改DropDownList控制导致页面发布回服务器的信息和GridView控件重新绑定到数据源使用新的筛选器来控制。

ASP.NET (C#)
<!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的属性DataView执行返回的对象Select方法。 有关详情,请参阅Expression

如果您将参数添加到FilterParameters集合中,您还可以包含格式字符串的占位符 (例如,"{0}")中要替换的参数值的表达式。 根据中的参数的索引替换占位符FilterParameters集合。 如果中的对象FilterParameters集合是null,该对象将替换为空字符串。

可以包括在参数FilterExpression属性。 如果参数是字符串或字符类型,将参数括在单引号内。 如果参数的数值类型,则不需要,引号引起来。 FilterParameters集合中包含的参数,其计算结果中找到占位符FilterExpression属性。

SqlDataSource控件支持筛选数据时,才在DataSet模式。

FilterExpression属性委托给FilterExpression的属性SqlDataSourceView与之关联的对象SqlDataSource控件。

适用于

.NET Framework

4.8 4.7.2 4.7.1 4.7 4.6.2 4.6.1 4.6 4.5.2 4.5.1 4.5 4.0 3.5 3.0 2.0

另请参阅

posted on 2019-01-16 00:27  qqhfeng16  阅读(753)  评论(0编辑  收藏  举报