Introduction
This article describes the use of filter expressions in junction with an SQL Data Source. Filter expressions may be applied to the data returned from the use of an SQL Data Source to limit what is displayed; the filter expression itself is quite similar in nature to a 'where' clause but there isn't any need to generate a new query or stored procedure as the filter expression is applied to the data collected using the SQL Data Source. For example, if one had an SQL Data Source configured to work with a stored procedure that pulled back all of the accounts for one zip code, the developer could use filter expressions to limit the account data shown by a range to dates, or a last name. This is not a replacement for parameterized queries, just another tool you can add to your arsenal.
Figure 1: Filtering Data in Grid View Control
Getting Started
In order to get started, unzip the included project and open the solution in the Visual Studio 2008 IDE. In the solution explorer, you should note these files (Figure 2):
Figure 2: Solution Explorer
The solution contains a single web application project called "FilteredSource"; the site contains a master page and a default web page.
Code: Master Page (FsSite.Master)
The master page contains a single table (2x2) with the top two cells merged to form an area for a banner, and a side bar on the left. The side bar contains a few hyperlink controls used to navigate to other pages. The lower right hand cell contains a single content panel which is used to display the default web page.
The code behind does not manage any data or perform any particular function and so there is no code to speak of; the html defines the layout of the master page and handles a few hyperlinks in the side bar. The class begins with the default imports (all of the imports are set by default):
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace FilteredSource
- {
- public partial class FsSite : System.Web.UI.MasterPage
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- }
- }
This default web page is used to display a GridView control and a few controls used to filter the data displayed in that GridView. The page also contains two SQLDataSource controls; one of which is used to populate the grid and the other to populate a drop down list of cities. Both data source controls bind to the example Northwind database in SQL Server 2005 for this example. One data source control is used to populate a drop down list with a distinct list of cities while the other is used to bind to the grid view which serves as the primary means for displaying the data; it is the second data source that is subject to filtering.
The class begins with the default imports.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
The next section contains the class declaration and default constructor and the namespace.
- namespace FilteredSource
- {
- public partial class Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if(Session["FiltExp"] != null)
- SqlDataSource1.FilterExpression = Session["FiltExp"].ToString();
- }
- protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
- {
- SqlDataSource1.FilterExpression = "city='" + DropDownList1.SelectedValue + "'";
- Session["FiltExp"] = "city='" + DropDownList1.SelectedValue + "'";
- }
- protected void btnShowByDate_Click(object sender, EventArgs e)
- {
- if (!String.IsNullOrEmpty(txtDay.Text) && !String.IsNullOrEmpty(txtMonth.Text) && !String.IsNullOrEmpty(txtYear.Text))
- {
- DateTime dt = new DateTime(Convert.ToInt32(txtYear.Text), Convert.ToInt32(txtMonth.Text), Convert.ToInt32(txtDay.Text));
- SqlDataSource1.FilterExpression = "BirthDate > #" + dt + "#";
- Session["FiltExp"] = "BirthDate > #" + dt + "#";
- }
- }
- protected void btnShowAll_Click(object sender, EventArgs e)
- {
- SqlDataSource1.FilterExpression = null;
- Session["FiltExp"] = null;
- }
The article is pretty short and simple. The intent was only to show how an SQLDataSource may be filtered on the fly using the Filter Expression property. This is not the only way to do this, one could for example use parameterized queries instead.

Michael ClintonPosted Jun 10, 2025, 8:47 PM
Can we add paging to this?
suporte oasysPosted Apr 5, 2012, 7:22 PM
You are really bad man!! I had been searching for code like yours for 2 (TWO) days! I'd found a LOT of junkie code and another lot of HUGE code to do the same you're doing with a few lines of code. Congratulation man!
abozar shabaniPosted Dec 5, 2010, 5:47 PM
how use DataBind in codebehind and FilterExpression
borith sovannPosted Oct 24, 2008, 4:10 AM
It does not show all records when i click on Show All button