asp.net.ph

Skip Navigation LinksHome > Data Access / ADO.NET > Data Source Controls Overview > Filtering Data Using Data Source Controls

Filtering Data Using Data Source Controls

Web Forms Server Controls   Controls You Can Use on Web Forms   ASP.NET Data Source Controls


Data source controls provide a number of data services that make it easier to add advanced capabilities to your applications. This includes filtering data based on search criteria that you specify. Filtering is especially convenient when working with cached data, because you can provide search capabilities without having to re-run queries or call methods to read data.

To filter data, a data source control must be configured in these ways:

When using the XmlDataSource control, you can filter data using XPath queries. For more information, see Filtering Data Using the XmlDataSource Control.

Setting the Filter Expression

You specify the filter to be applied to the data returned by an ObjectDataSource, SqlDataSource, or AccessDataSource control by setting the data source control’s FilterExpression property. The syntax for the filter expression is based on the syntax of the Expression property of the DataColumn class. The filter expression is applied when the data source control’s Select method is called.

Providing Filter Parameters

You can provide a parameterized filter expression for an ObjectDataSource, SqlDataSource, or AccessDataSource control, which enables you to provide filter values at run time without writing any code to explicitly set the FilterExpression property. You specify filter expression parameters using the data source control’s FilterParameters collection. The parameters can retrieve data from controls, the QueryString object, session state, user profile properties, and so on. For information on the types of parameters that can be used in the FilterParameters collection, see Using Parameters with Data Source Controls.

In the filter expression, you create placeholders that correspond to items in the data source control’s FilterParameters collection. The placeholders are indexed, or numbered, with 0 representing the first parameter in the collection. You specify a placeholder in the filter expression by placing the index of the filter parameter within opening and closing brackets { }, as shown in the following example:

Country = '{0}’ AND LastName LIKE '%{1}%'

NOTE: Because values from the FilterParameters collection are substituted into the FilterExpression string without encoding, you should validate all filter parameter values before applying a filter. You can use the Filtering event of the data source control to access and validate filter parameter values before the filter is applied.

The following example shows an AccessDataSource control that includes a filter parameter. The parameter value used in the FilterExpression property is obtained at run time from the property value of a control defined elsewhere on the page.

<asp:accessdatasource id="Products" runat="server"
   datafile="~/app_data/gear.mdb"
   selectcommand="SELECT Type, ProductId, Brand, Model, Description, Price FROM Products"
   filterexpression="Type='{0}’">

   <filterparameters>
      <asp:controlparameter controlid="filterType" propertyname="Text" />
   </filterparameters>

</asp:accessdatasource>

 Show me 

See Also

Using Parameters with Data Source Controls   How a Data Source Control Creates Parameters for Data-bound Fields



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note