ManuelAbadia.com
ExtendedObjectDataSource samples
Samples



Product Information

Constructor Parameters

By default, if the ObjectDataSource is going to call an instance method it will create an instance of the type specified by the TypeName property. However, the instance will be created using the default constructor. If you want to use another constructor, you have to handle the ObjectCreating event and write the code to create the instance.

The CompatObjectDataSource and the ExtendedObjectDataSource have a property called ConstructorParameters that allow the developer to pass parameters to the constructor without writing any code.

To be able to pass complex parameters to the constructor, two new type of parameters are available:

  • InstanceParameter: That returns instances of a type, specifying a TypeName and the ConstructorParameters for the type.
  • MethodParameter: That returns the value obtained from a method call, specifying a TypeName, ConstructorParameters for the type, MethodName and MethodParameters for the method.

The following example will pass two parameters to the constructor of the ProductsDAL type:

/// <summary>Initializes a new instance of the ProductsDAL class.</summary>

/// <param name="httpContext">Current Http context.</param>

/// <param name="dbContext">Data base context for the related tables.</param>

public ProductsDAL(HttpContext httpContext, DataBaseContext dbContext)

The parameters will be the current HttpContext and a database context for the products table. The HttpContext will be obtained using the Helper method Helpers.GetCurrentHttpContext(). The database context will be a new instance of the DataBaseContext class. The constructor parameters to create an instance of the ProductsDAL class using the constructor specified above are:

<ConstructorParameters>

    <manu:MethodCallParameter Name="httpContext" MethodName="GetCurrentHttpContext"
        TypeName="Manu.Helpers, __code" />

    <manu:InstanceParameter Name="dbContext"
       
TypeName="Manu.DBUtils.DataBaseContext, __code">

          <ConstructorParameters>

               <asp:Parameter Name="schema" Type="String" DefaultValue="Products" />

          </ConstructorParameters>

    </manu:InstanceParameter>

</ConstructorParameters>

The example use a CompatObjectDataSource to show the products using an instance of the ProductsDAL class created with the constructor shown above:

IdNameDescriptionPrice
1Product 1Full description for product number 12
2Product 2Full description for product number 24
3Product 3Full description for product number 36
4Product 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="ConstructorParameters.aspx.cs" Inherits="ConstructorParameters" Title="Constructor Parameters" %>

 

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

 

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

    <h1>Constructor Parameters</h1>

 

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

        AutoGenerateColumns="False" CssClass="gridview" BackColor="#335789"

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

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

        <Columns>

            <asp:BoundField DataField="Id" HeaderText="Id" 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="CompatObjectDataSource1" runat="server"

        TypeName="ProductsDAL" DataObjectTypeName="Product"

        SelectMethod="LoadAll" SelectCountMethod="CountAll"

        MaximumRowsParameterName="maxRows" StartRowIndexParameterName="startIndex"

        EnablePaging="true" SortParameterName="sortedBy">

        <ConstructorParameters>

            <manu:MethodCallParameter Name="httpContext"
                MethodName="GetCurrentHttpContext"
                TypeName
="Manu.Helpers, __code" />

            <manu:InstanceParameter Name="dbContext"

                TypeName="Manu.DBUtils.DataBaseContext, __code">

                <ConstructorParameters>

                    <asp:Parameter Name="schema" Type="String" DefaultValue="Products" />

                </ConstructorParameters>

            </manu:InstanceParameter>

        </ConstructorParameters>

    </manu:CompatObjectDataSource>

</asp:Content>


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


using System;

using System.Web.UI;

 

public partial class ConstructorParameters : Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

}