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">
<ContentTemplate>
Select
State :- <asp:DropDownList ID="ddlState" DataSourceID="objState" AppendDataBoundItems="true"
AutoPostBack="true"
DataTextField="State"
DataValueField="State"
runat="server"
>
<asp:ListItem
Text="--Select--"
Value="0"
/>
</asp:DropDownList>
<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"
BorderStyle="Groove">
<RowStyle
BackColor="#EFF3FB"
/>
<Columns>
<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"
/>
</Columns>
<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:GridView>
<asp:ObjectDataSource
ID="objCustomer"
runat="server"
EnableCaching="true"
CacheDuration="Infinite"
TypeName="ObjFilter"
SelectMethod="GetCustomer"
FilterExpression="[State]='{0}'"
onfiltering="objCustomer_Filtering">
<FilterParameters>
<asp:ControlParameter
Name="State"
ControlID="ddlState"
/>
</FilterParameters>
</asp:ObjectDataSource>
</ContentTemplate>
</asp:UpdatePanel> |
|
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) { da.Fill(ds); } return ds; }
public DataSet GetState() { string sqlQuery = "SELECT DISTINCT State FROM Customer"; SqlDataAdapter da = new SqlDataAdapter(sqlQuery, conStr); DataSet ds = new DataSet();
using (da) { da.Fill(ds); } return ds; } }
|
Output as below :-