ExtendedObjectDataSource samples

Product Information

Filtering with strongly typed parameters

The ObjectDataSource was designed to support filtering only if the data returned by the SelectMethod could return a DataView, using the RowFilter property. However, if we are not using a DataView, sometimes is useful to use the indidivual strongly typed filtering parameters instead of a string with the filter expression. The CompatObjectDataSource and the ExtendedObjectDataSource support this kind of filtering. If the IndividualFilterParameters property is set to true, the SelectMethod will be passed a parameter for each filter parameter in the FilterParameters collection (the parameter will be named the same as the Name property of the filter parameter). The parameter specified by the FilterParameterName property will not be passed to the SelectMethod but it needs to be set for compatibility reasons because the controls check if the FilterParameterName is set to something to figure out if filtering is enabled.

The Parameter class support most common simple types but in case there is need for other types it can be extended to support more types. There are more types of parameters than can be used: ControlParameter, CookieParameter, ProfileParameter, FormParameterSessionParameter, QueryStringParameter.

The following sample uses strongly typed filter parameters.  Two filter parameters are defined (filteredById and filteredByName) and the signature of the SelectMethod is:

public static List<Product> LoadFilteredEx(int startIndex, int maxRows, string sortedBy, int filteredById, string filteredByName, out int totalRowCount)

id >:   
name like:

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

Sample source code

The ASPX file for this sample is:

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


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


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

    <h1>Filtering with strongly typed parameters</h1>


    id &gt;:

        <asp:TextBox ID="txtFilteredById" runat="server">0</asp:TextBox>

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

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

        <br />

    name like:

    <asp:TextBox ID="txtFilteredByName" runat="server">Product</asp:TextBox>

    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 

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

    <br />

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

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

        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">


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

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

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

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


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

            <HeaderStyle ForeColor="White" />

            <PagerStyle ForeColor="White" />


        <manu:ExtendedObjectDataSource ID="ObjectDataSource1" runat="server"


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

            SelectMethod="LoadFilteredEx" TypeName="ProductsDAL" UpdateMethod="Edit"

            OnDeleted="ObjectDataSource1_Deleted" OnUpdated="ObjectDataSource1_Updated"

            MaximumRowsParameterName="maxRows" StartRowIndexParameterName="startIndex"

            TotalRowCountParameterName="totalRowCount" EnablePaging="True"

            SortParameterName="sortedBy" FilterExpression="{0}{1}"



                <asp:ControlParameter ControlID="txtFilteredById" Name="filteredById"
="Int32" defaultValue="0"  />

                <asp:ControlParameter ControlID="txtFilteredByName" Name="filteredByName" 
="String" defaultValue="Product"  />



        <br />

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


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

using System;

using System.Web.UI;

using System.Web.UI.WebControls;


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


    protected void Page_Load(object sender, EventArgs e)





    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;