ExtendedObjectDataSource samples

Product Information

DataObjectType instantiation control

In this sample we use a CompatObjectDataSource to show, edit and update data using a GridView and to insert data using a DetailsView. Note that we are using custom objects as the parameters to the CompatObjectDataSource. Also note that the GridView is not showing all the properties of the products (the Description and Price are missing).

In scenarios like this using the ObjectDataSource causes the update to not behave properly. When the update operation takes place, the ObjectDataSource instantiates a new object (a Product in this case), and then sets its properties with the values from the control using the data source. In our sample, the GridView passes a Product instance with the Id and Name properties set, but the missing properties (Description and Price) are not set and have the default values. When the update completes we have lost the proper values of the properties that are not shown!

With the CompatObjectDataSource and the ExtendedObjectDataSource, we have the option to supply a custom instance to the update method instead of creating a new one, so the previous scenario will work if we supply a product instance with the values read from the database. Before calling the update method, the CompatObjectDataSource and the ExtendedObjectDataSource raise the DataObjectCreating event. To supply a custom instance, the ObjectInstance property of the event arguments has to be assigned.


Edit Delete1Product 1
Edit Delete2Product 2
Edit Delete3Product 3
Edit Delete4Product 4

Insert Cancel

Sample source code

The ASPX file for this sample is:

<%@ Page Language="C#" MasterPageFile="~/DefaultMaster.master" AutoEventWireup="true" CodeFile="DataObjectTypeControl.aspx.cs" Inherits="DataObjectTypeControl" Title="DataObjectType instantiation control" %>


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


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

    <h1>DataObject instantiation control</h1>


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

        AutoGenerateColumns="False" AutoGenerateDeleteButton="True"
="True" CssClass="gridview" BackColor="#335789"
="#133769" BorderStyle="Solid" BorderWidth="1px"

        DataKeyNames="Id" DataSourceID="ObjectDataSource1" PageSize="4">


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

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


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

        <HeaderStyle ForeColor="White" />

        <PagerStyle ForeColor="White" />


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

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

        SelectMethod="LoadAll" TypeName="ProductsDAL" UpdateMethod="Edit"

        OnDeleted="ObjectDataSource1_Deleted" OnInserted="ObjectDataSource1_Inserted"

        OnSelected="ObjectDataSource1_Selected" OnUpdated="ObjectDataSource1_Updated"

        MaximumRowsParameterName="maxRows" StartRowIndexParameterName="startIndex"

        EnablePaging="true" SelectCountMethod="CountAll" SortParameterName="sortedBy"




    <br />

    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
="Id" DataSourceID="ObjectDataSource1" DefaultMode="Insert" 
="50px" Width="125px" CssClass="htmltable">


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

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

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

            <asp:CommandField ShowInsertButton="True" />


        <CommandRowStyle ForeColor="White" />


    <br />

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


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

using System;

using System.ComponentModel;

using System.Web.UI.WebControls;

using Manu.Web.UI.WebControls;


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


    protected void Page_Load(object sender, EventArgs e)




    protected void ObjectDataSource1_Selected(object s, ObjectDataSourceStatusEventArgs e)




    protected void ObjectDataSource1_Inserted(object s, ObjectDataSourceStatusEventArgs e)


        lbResult.Text = String.Format("Affected Rows = {0}. Generated Id = {1}"
                                                e.AffectedRows, (int)e.ReturnValue);



    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 ObjectDataSource1_DataObjectCreating(object sender,


        // load the product before the update in order to
        // preserve the properties that aren't shown in the GridView

        if (e.MethodType == DataObjectMethodType.Update) {

            ProductsDAL prodsDAL = new ProductsDAL();

            e.ObjectInstance = prodsDAL.LoadById(Convert.ToInt32(e.Keys["Id"]));




    protected void ObjectDataSource1_DataObjectCreated(object sender,