Asp. NET ObjectDataSource Control DataObjectTypeName Properties _ Basic Application

Source: Internet
Author: User

One, ObjectDataSource control description

Gets or sets the name of a class that is used by the ObjectDataSource control to update, insert, or delete parameters in a data operation instead of passing individual values from a data-bound control.

Instead of specifying multiple parameters to be passed to the Update, Insert, and Delete methods, you can create an object that accumulates values for more than one data field. Only this object is passed to the method, not multiple parameters.

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

Second, the use of DataObjectTypeName properties

The type specified by the DataObjectTypeName property must have a default constructor with no arguments so that the ObjectDataSource control can create an instance of this type. This type must also have properties that can be set to allow the ObjectDataSource control to populate the object with values passed by the data-bound control. 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 method specified by the InsertMethod and DeleteMethod properties must have a different The parameter of the type specified in the DataObjectTypeName property. If the ConflictDetection property is set to a overwritechanges value, the method specified by the UpdateMethod property must have a parameter of the type specified in the DataObjectTypeName property. If the ConflictDetection property is set to a CompareAllValues value, the method specified by the UpdateMethod property must have two parameters for the type specified in the DataObjectTypeName property. The first parameter contains the original value, and the second parameter contains the new value.

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

Third, sample code

The following code example demonstrates how to use the DataObjectTypeName property to implement a type that merges all parameter values into one object. The selection method for the Aggregatedata class returns a DataTable object with two columns named name and number. Similarly, the NewData class defines two read/write property Name and number. The Insert method of the Aggregatedata class takes a parameter of the NewData type. The ObjectDataSource TypeName property is set to the Aggregatedata,dataobjecttypename property 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>  

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 {namevalue;}
    set {namevalue = value;}
      public int Number {get {numbervalue;}
    set {Numbervalue = value;} }

  }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.