ObjectDataSource using Filter

The Filter expression is applied to the data returned by the control’s select method. A filter is particularly useful when used with caching. You can load all the data into the cache and then apply different filters to the cached data. 

Download complete source code here.

In below example, I am using Dropdown list to display customer State, and the Grid View displaying all matching state.

<asp:UpdatePanel ID="up1" runat="server">


Select State :-

<asp:DropDownList ID="ddlState"



                  AutoPostBack="true" DataTextField="State"

                  DataValueField="State" runat="server" >

<asp:ListItem Text="--Select--" Value="0" />


<asp:ObjectDataSource ID="objState" TypeName="ObjFilter" SelectMethod="GetState"

EnableCaching="true" CacheDuration="Infinite" runat="server" />

<br />

<br />

<asp:GridView ID="gvFilter" runat="server" AutoGenerateColumns="False" DataSourceID="objCustomer"

CellPadding="4" DataKeyNames="CustomerID" ForeColor="#333333" GridLines="None"


<RowStyle BackColor="#EFF3FB" />


<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />

<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />

<asp:BoundField DataField="Desgination" HeaderText="Desgination" SortExpression="Desgination" />

<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />

<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />

<asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />

<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />


<FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />

<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

<EditRowStyle BackColor="#2461BF" />

<AlternatingRowStyle BackColor="White" />


<asp:ObjectDataSource ID="objCustomer" runat="server" EnableCaching="true" CacheDuration="Infinite"

TypeName="ObjFilter" SelectMethod="GetCustomer"

FilterExpression="[State]='{0}'" onfiltering="objCustomer_Filtering">


<asp:ControlParameter Name="State" ControlID="ddlState" />





However, one thing to note about this is that, initially  dropdown will have value as “0” text as “—Select—“. In that case we can perform operation on ObjectDataSource Filtering event. We can get the Filter value from e.ParameterValues[0], so if value is 0 then I can assign an default value as e.ParameterValues[0] = "AP"; or I can cancel existing filtering using e.Cancel.


protected void objCustomer_Filtering(object sender, ObjectDataSourceFilteringEventArgs e)



     if (Convert.ToString(e.ParameterValues[0]) == "0")


             /*Assinging Default value if not selected from dropdown list*/

            //e.ParameterValues[0] = "AP";


            /* You can also cancel filter if you don't want to continue your filter*/

            //e.Cancel = true;




ObjFilter.cs class looks like below one :-

using System;

using System.Data;

using System.Data.SqlClient;

using System.Web.Configuration;

public class ObjFilter


    private readonly string conStr;

      public ObjFilter()


        conStr = WebConfigurationManager.ConnectionStrings["MyBlogConnectionString"].ConnectionString;



    public DataSet GetCustomer()


            string sqlQuery = "SELECT CustomerID,FirstName,LastName,Desgination,";

            sqlQuery +="Address,City,State,Country FROM Customer";

            SqlDataAdapter da = new SqlDataAdapter(sqlQuery, conStr);

            DataSet ds = new DataSet();


            using (da)




            return ds;    


    public DataSet GetState()


            string sqlQuery = "SELECT DISTINCT State FROM Customer";

            SqlDataAdapter da = new SqlDataAdapter(sqlQuery, conStr);

            DataSet ds = new DataSet();


            using (da)




            return ds;




Output as below :-


No Comments