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

属性值

String

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

例外

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

示例

下面的代码示例演示如何从 Northwind 数据库检索数据,并使用字符串和FilterParameters集合对其进行筛选FilterExpression。 每当执行该方法以检索数据时Select,将FilterExpression应用该属性。 在此示例中, FilterExpression 包含筛选器参数的占位符,该参数包含在集合中 FilterParameters 。 此外,筛选器参数是 ControlParameter 绑定到 SelectedValue 控件属性 DropDownList 的对象。 DropDownList由于控件的属性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

适用于

另请参阅