ObjectDataSource.FilterExpression Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets or sets a filtering expression that is applied when the method that is specified by the SelectMethod property is called.

Namespace:   System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

member FilterExpression : string with get, set

Property Value

Type: System.String

A string that represents a filtering expression that is applied when data is retrieved by using the method or function identified by the SelectMethod property.

Exception Condition

The FilterExpression property was set and the Select method does not return a DataSet or DataTable.

The ObjectDataSource control supports filtering data only when the Select method returns a DataSet or DataTable object.

The syntax that is used for the FilterExpression property is a format string–style expression. The filter expression syntax is the same syntax that is accepted by the Expression property. If you add parameters to the FilterParameters collection, you can also include format string placeholders. For example, include "{0}" in the expression to substitute for parameter values. The placeholders are replaced according to the index of the parameter in the FilterParameters collection.

You can include parameters in the FilterExpression property. If the type of 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 FilterExpression property delegates to the FilterExpression property of the ObjectDataSourceView object that is associated with the ObjectDataSource control.


You should validate any filter parameter value that you receive from the client. The runtime simply substitutes the parameter value into the filter expression and applies it to the DataSet or DataTable object that is returned by the Select method. 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.

The following code example demonstrates how to display filtered data using an ObjectDataSource control to retrieve data from a middle-tier business object and a GridView control to display the results. The ObjectDataSource control can filter data only when the method that retrieves the data retrieves it as a DataSet or DataTable object. For this reason, the SelectMethod property identifies a business object method that retrieves data as a DataSet.

The code example consists of a TextBox control, a GridView control, the ObjectDataSource control, and a Submit button. By default, the TextBox is populated with the name of one of the Northwind Traders employees. The GridView displays information about the employee that is identified by the name in the TextBox. To retrieve data on another employee, enter the full name of the employee in the TextBox, and then click the Submit button.

The FilterExpression property specifies an expression that is used to filter the data that is retrieved by the method that is specified by the SelectMethod property. It uses parameter placeholders that are evaluated to the parameters that are contained in the FilterParameters collection. In this example, the parameter placeholder is bounded by single quotation marks because the type of the parameter is a string type that might contain spaces. If the type of the parameter is a numeric or date type, bounding quotation marks are not required.

System_CAPS_security Security Note

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

No code example is currently available or this language may not be supported.

The following code example provides an example of a middle-tier business object that the preceding code example uses. The code example consists of two basic classes:

  • The EmployeeLogic class, which is a class that encapsulates business logic.

  • The NorthwindEmployee class, which is a model class that contains only the basic functionality that is required to load and persist data from the data tier.

For simplicity, the EmployeeLogic class creates a static set of data, rather than retrieving the data from a data tier. It is also helpful for this example, because the sample relies on you to provide the full name of a Northwind Traders employee to demonstrate filtering. For a complete working example, you must compile and use these classes with the Web Forms code examples that are provided.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 2.0
Return to top