ASP.NET中 ObjectDataSource控制項的DataObjectTypeName屬性_基礎應用

來源:互聯網
上載者:User

一、ObjectDataSource 控制項說明

擷取或設定某個類的名稱,ObjectDataSource 控制項將該類用於更新、插入或刪除資料操作中的參數,而不是從資料繫結控制項傳遞個別的值。

您不用指定傳遞給 Update、Insert 和 Delete 方法的多個參數,而是可以建立一個累計多個資料欄位值的對象。僅給方法傳遞這一個對象,而不是多個參數。

綁定到資料繫結控制項的 ObjectDataSource 控制項的預設行為是,資料繫結控制項為資料來源中的每個參數建立一個 Parameter 對象。如果業務對象有很多欄位,則結果方法也有很多欄位。DataObjectTypeName 屬性允許您為每個資料欄位都指定一個具有屬性的類型。這樣,運行時不是給方法傳遞多個參數,而是建立一個對象並設定它的所有屬性。這一個對象添加到方法調用的參數集合中。

二、DataObjectTypeName 屬性的使用

DataObjectTypeName 屬性指定的類型必須有一個不帶參數的預設建構函式,以便 ObjectDataSource 控制項可以建立此類型的執行個體。此類型還必須具有可設定的屬性,允許 ObjectDataSource 控制項用資料繫結控制項傳遞的值填充對象。ObjectDataSource 控制項的屬性名稱應該與資料繫結控制項傳遞的值的參數名完全符合。

當設定了 DataObjectTypeName 屬性並且 ObjectDataSource 控制項與資料繫結控制項關聯時,由 InsertMethod 和 DeleteMethod 屬性指定的方法必須各有一個在 DataObjectTypeName 屬性中指定的類型的參數。如果 ConflictDetection 屬性設定為 OverwriteChanges 值,則由 UpdateMethod 屬性指定的方法必須有一個在 DataObjectTypeName 屬性中指定的類型的參數。如果 ConflictDetection 屬性設定為 CompareAllValues 值,則由 UpdateMethod 屬性指定的方法必須有兩個在 DataObjectTypeName 屬性中指定的類型的參數。第一個參數包含原始值;第二個參數包含新值。

DataObjectTypeName 屬性委託給與 ObjectDataSource 控制項關聯的 ObjectDataSourceView 的 DataObjectTypeName 屬性。

三、範例程式碼

下面的程式碼範例示範如何使用 DataObjectTypeName 屬性,實現一個將所有參數值合并為一個對象的類型。AggregateData 類的選擇方法返回一個有兩個名為 Name 和 Number 的列的 DataTable 對象。同樣,NewData 類定義兩個讀/寫屬性 Name 和 Number。AggregateData 類的 Insert 方法帶 NewData 類型的一個參數。ObjectDataSource 的 TypeName 屬性設定為 AggregateData,DataObjectTypeName 屬性設定為 NewData。

前台代碼:

<%@ 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>

後台代碼:

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; }    }  }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.