ManuelAbadia.com
ExtendedObjectDataSource samples
Samples



Product Information

Advanced Filtering Support

The ObjectDataSource only allows to use filtering if the Select operation returns a DataSet, DataTable or a DataView, performing the filtering in the page using the RowFilter property of the underlying DataView.

However, the CompatObjectDataSource and the ExtendedObjectDataSource support a filter parameter specified using the FilterParameterName property that can be passed to the select method to perform filtering at the database level or at any other level that you want and doesn't restrict you to return any type. if the FilterParameterName hasn't been specified, the behaviour is similar to the ObjectDataSource.

The filter expression is built on the fly using the FilterExpression property that can have as many placeholders as needed ({0} for the first placeholder, {1} for the second placeholder, etc) and when the Select operation is performed, each placeholder is substituted by the associated parameter in the FilterParameters collection.

For example, you can have a parameter for filtering called filteredBy (that will be present in the Select method), and the FilterExpression could be:

"(name like %{0}%) AND (address like %{1}%)"

The FilterParameters collection will have two control parameters bound to two TextBox controls.

The following example shows how to use filtering with one parameter bound to the TextBox and a filter expression of "{0}"

Filter expression:  

 IdNameDescriptionPrice
Edit Delete1Product 1Full description for product number 12
Edit Delete2Product 2Full description for product number 24
Edit Delete3Product 3Full description for product number 36
Edit Delete4Product 4Full description for product number 48
123456789

Sample source code

The ASPX file for this sample is:


<%@ Page Language="C#" MasterPageFile="~/DefaultMaster.master" AutoEventWireup="true" CodeFile="Filtering.aspx.cs" Inherits="Filtering" Title="Untitled Page" %>

 

<%@ Register Assembly="ExtendedObjectDataSource" Namespace="Manu.Web.UI.WebControls" TagPrefix="manu" %>

 

<asp:Content ID="Content1" ContentPlaceHolderID="Content" Runat="Server">

    <h1>Advanced Filtering</h1>

    Filter expression:

        <asp:TextBox ID="txtFilter" runat="server">id &gt; 0</asp:TextBox>

        <asp:RequiredFieldValidator ID="rfvFilter" runat="server"
            ControlToValidate
="txtFilter"

            Display="Dynamic" ErrorMessage="Please enter a expression to filter data">

        </asp:RequiredFieldValidator>&nbsp;

        <asp:Button ID="bFilter" runat="server" OnClick="bFilter_Click" Text="Refresh
        results with current filter"
/>

        <br /><br />

        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
        AutoGenerateColumns
="False"

        AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"

        DataKeyNames="Id" DataSourceID="ObjectDataSource1" PageSize="4"
        CssClass
="gridview"

        BackColor="#335789" BorderColor="#133769" BorderStyle="Solid" BorderWidth="1px"

        EmptyDataText="Sorry, there are no products that matched your search criteria">

            <Columns>

                <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True"
                SortExpression
="Id" />

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

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

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

            </Columns>

            <RowStyle BackColor="White" ForeColor="#436799" />

            <HeaderStyle ForeColor="White" />

            <PagerStyle ForeColor="White" />

        </asp:GridView>

        <manu:CompatObjectDataSource ID="ObjectDataSource1" runat="server"
            DataObjectTypeName
="Product" FilterParameterName="filteredBy"

            DeleteMethod="Delete" InsertMethod="Add" OldValuesParameterFormatString="old_
            {0}"

            SelectMethod="LoadFiltered" TypeName="ProductsDAL" UpdateMethod="Edit"

            OnDeleted="ObjectDataSource1_Deleted" OnUpdated="ObjectDataSource1_Updated"
            MaximumRowsParameterName
="maxRows" StartRowIndexParameterName="startIndex"
            EnablePaging
="true" SelectCountMethod="CountAll" SortParameterName="sortedBy"
            FilterExpression
="{0}">

            <FilterParameters>

                <asp:SessionParameter Name="filterValue" SessionField="filteredBy" />

            </FilterParameters>

        </manu:CompatObjectDataSource>

        <br />

        <asp:Label ID="lbResult" runat="server" EnableViewState="False"></asp:Label>

</asp:Content>


The code-behind file (ASPX.CS) is:


using System;

using System.Data;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

public partial class Filtering : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        // the first time, init the filter

        if (!this.IsPostBack) {

            Session["filteredBy"] = txtFilter.Text;

        }

    }

 

    protected void ObjectDataSource1_Updated(object s, ObjectDataSourceStatusEventArgs e)

    {

        lbResult.Text = "Affected Rows = " + e.AffectedRows;

    }

 

    protected void ObjectDataSource1_Deleted(object s, ObjectDataSourceStatusEventArgs e)

    {

        lbResult.Text = "Affected Rows = " + e.AffectedRows;

    }

 

    protected void bFilter_Click(object sender, EventArgs e)

    {

          GridView1.PageIndex = 0;

        // changing the session variable forces a call to the select method on PreRender

        Session["filteredBy"] = txtFilter.Text;

    }

}