Editare

Partajați prin


SqlDataSource.FilterExpression Property

Definition

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

Property Value

A string that represents a filtering expression applied when data is retrieved using the Select(DataSourceSelectArguments) method.

Exceptions

The FilterExpression property was set and the SqlDataSource is in DataReader mode.

Examples

The following code example demonstrates how to retrieve data from the Northwind database and filter it using a FilterExpression string and the FilterParameters collection. The FilterExpression property is applied any time the Select method is executed to retrieve data. In this example, the FilterExpression contains a placeholder for a filter parameter, which is contained in the FilterParameters collection. In addition, the filter parameter is a ControlParameter object that is bound to the SelectedValue property of the DropDownList control. 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>

Remarks

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. 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. For more information, see Expression.

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. The placeholders are replaced according to the index of the parameter in the FilterParameters collection. If an object in the FilterParameters collection is null, the object will be replaced by an empty string.

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. The FilterParameters collection contains the parameters that are evaluated for the placeholders that are found in the FilterExpression property.

The SqlDataSource control supports filtering data only when in the DataSet mode.

The FilterExpression property delegates to the FilterExpression property of the SqlDataSourceView object that is associated with the SqlDataSource control.

Applies to

See also