ASP. DataObjectTypeName property of ObjectDataSource control in NET

  • 2021-07-18 07:44:21
  • OfStack

1. ObjectDataSource control description

Gets or sets the name of the class that the ObjectDataSource control uses to update, insert, or delete parameters in data operations instead of passing individual values from the data-bound control.

Instead of specifying multiple parameters passed to the Update, Insert, and Delete methods, you can create an object that accumulates multiple data field values. Only pass this 1 object to the method, not multiple parameters.

The default behavior of an ObjectDataSource control bound to a data-bound control is that the data-bound control creates one Parameter object for each parameter in the data source. If the business object has many fields, the result method also has many fields. The DataObjectTypeName property allows you to specify one type with an attribute for each data field. In this way, instead of passing multiple parameters to the method, the runtime creates an object and sets all its properties. This 1 object is added to the parameter collection of the method call.

2. Use of the DataObjectTypeName attribute

The type specified by the DataObjectTypeName property must have a default constructor without arguments so that the ObjectDataSource control can create an instance of this type. This type must also have settable properties that allow ObjectDataSource controls to populate objects with values passed by data-bound controls. The property name of the ObjectDataSource control should exactly match the parameter name of the value passed by the data-bound control.

When the DataObjectTypeName property is set and the ObjectDataSource control is associated with a data-bound control, the methods specified by the InsertMethod and DeleteMethod properties must each have one parameter of the type specified in the DataObjectTypeName property. If the ConflictDetection property is set to an OverwriteChanges value, the method specified by the UpdateMethod property must have 1 parameter of the type specified in the DataObjectTypeName property. If the ConflictDetection property is set to an CompareAllValues value, the method specified by the UpdateMethod property must have two parameters of the type specified in the DataObjectTypeName property. The first parameter contains the original value; The second parameter contains the new value.

The DataObjectTypeName property is delegated to the DataObjectTypeName property of the ObjectDataSourceView associated with the ObjectDataSource control.

3. Sample code

The following code example demonstrates how to use the DataObjectTypeName property to implement a type that combines all parameter values into one object. The selection method of the AggregateData class returns an DataTable object with two columns named Name and Number. Similarly, the NewData class defines two read/write attributes, Name and Number. The Insert method of the AggregateData class takes one parameter of type NewData. The TypeName property of ObjectDataSource is set to AggregateData, and the DataObjectTypeName property is set to NewData.

Foreground code:


<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS" Assembly="Samples.AspNet.CS" %>
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>ObjectDataSource - DataObjectTypeName Property Example</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:DetailsView 
      ID="DetailsView1" 
      runat="server" 
      AllowPaging="True" 
      AutoGenerateInsertButton="True"
      DataSourceID="ObjectDataSource1" 
      Height="50px" 
      Width="125px">
    </asp:DetailsView>
    <asp:ObjectDataSource 
      ID="ObjectDataSource1" 
      runat="server" 
      DataObjectTypeName="Samples.AspNet.CS.NewData"
      InsertMethod="Insert" 
      SelectMethod="Select" 
      TypeName="Samples.AspNet.CS.AggregateData">
    </asp:ObjectDataSource>
  </div>
  </form>
</body>
</html>

Background code:


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Samples.AspNet.CS
{

  /// <summary>
  /// Summary description for AggregateData
  /// </summary>
  public class AggregateData
  {

    public AggregateData()
    {

    }

    static DataTable table;

    private DataTable CreateData()
    {
      table = new DataTable();
      table.Columns.Add("Name", typeof(string));
      table.Columns.Add("Number", typeof(int));
      table.Rows.Add(new object[] { "one", 1 });
      table.Rows.Add(new object[] { "two", 2 });
      table.Rows.Add(new object[] { "three", 3 });
      return table;
    }

    public DataTable Select()
    {
      if (table == null)
      {
        return CreateData();
      }
      else
      {
        return table;
      }
    }

    public int Insert(NewData newRecord)
    {
      table.Rows.Add(new object[] { newRecord.Name, newRecord.Number });
      return 1;
    }
  }

  public class NewData
  {
    private string nameValue;
    private int numberValue;

    public string Name
    {
      get { return nameValue; }
      set { nameValue = value; }
    }

    public int Number
    {
      get { return numberValue; }
      set { numberValue = value; }
    }

  }
}


Related articles: