Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
FilterExpression Property

ObjectDataSourceView.FilterExpression Property

Gets or sets a filtering expression that is applied when the business object method that is identified 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 applied when data is retrieved using the business object method identified by the SelectMethod property.

ExceptionCondition
NotSupportedException

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

The ObjectDataSource control supports filtering data only when the Select method returns a DataSet, DataView, 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 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.

You can include parameters in the FilterExpression. If the type of the parameter is a string or character type, enclose the parameter in single quotation marks. Quotation marks are not needed if the parameter is a numeric type.The FilterParameters collection contains the parameters that are evaluated for the placeholders found in the FilterExpression.

The value of the FilterExpression property is stored in view state.

Security noteSecurity Note

It is recommended that you 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 DataView 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.

This section contains two code examples. The first 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 second code example provides an example of the middle-tier business object that is used in the first code example.

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 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, 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, in the TextBox, enter the full name of the employee, and then click the Submit button.

The FilterExpression property specifies an expression that is used to filter the data that is retrieved 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.

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 page code examples that are provided.

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0
Show:
© 2015 Microsoft