ObjectDataSource.DataObjectTypeName 屬性

定義

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

public:
 property System::String ^ DataObjectTypeName { System::String ^ get(); void set(System::String ^ value); };
public string DataObjectTypeName { get; set; }
member this.DataObjectTypeName : string with get, set
Public Property DataObjectTypeName As String

屬性值

String

部分或完整類別名稱,識別 ObjectDataSource 可做為參數用於 Insert()Update()Delete() 作業的物件型別。 預設為空字串 ("")。

範例

區段包含兩個程式碼範例。 第一個程式碼範例示範如何使用 屬性,實作將所有參數值結合成一個物件的型別 DataObjectTypeName 。 第二個程式碼範例顯示使用第一個程式碼範例中所使用兩個類別的網頁。

下列程式碼範例示範如何使用 屬性,實作將所有參數值結合成一個物件的型別 DataObjectTypeName 。 類別的 AggregateData select 方法會傳回名為 和 NumberNameDataTable 資料行的 物件。 同樣地,類別 NewData 會定義兩個讀取/寫入屬性和 Name Number 。 類別 InsertAggregateData 方法會採用 類型的 NewData 一個參數。 TypeNameObjectDataSource 屬性設定為 AggregateData ,而 DataObjectTypeName 屬性設定為 NewData

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; }
        }
    }
}
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls

Namespace Samples.AspNet.VB

    Public Class AggregateData

        Public Sub New()
        End Sub

        Shared table As DataTable

        Private Function CreateData() As DataTable
            table = New DataTable()
            table.Columns.Add("Name", GetType(String))
            table.Columns.Add("Number", GetType(Integer))
            table.Rows.Add(New Object() {"one", 1})
            table.Rows.Add(New Object() {"two", 2})
            table.Rows.Add(New Object() {"three", 3})
            Return table
        End Function

        Public Function SelectMethod() As DataTable
            If table Is Nothing Then
                Return CreateData()
            Else
                Return table
            End If
        End Function


        Public Function Insert(ByVal newRecord As NewData) As Integer

            table.Rows.Add(New Object() {newRecord.Name, newRecord.Number})
            Return 1
        End Function
    End Class


    Public Class NewData

        Private nameValue As String
        Private numberValue As Integer

        Public Property Name() As String
            Get
                Return nameValue
            End Get
            Set(ByVal value As String)
                nameValue = value
            End Set
        End Property

        Public Property Number() As Integer
            Get
                Return numberValue
            End Get
            Set(ByVal value As Integer)
                numberValue = value
            End Set
        End Property
    End Class
End Namespace

下列程式碼範例顯示使用上述程式碼範例中所使用的兩個類別的網頁。

<%@ 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>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB" Assembly="Samples.AspNet.VB" %>
<%@ Page Language="VB" %>

<!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.VB.NewData"
            InsertMethod="Insert" 
            SelectMethod="SelectMethod" 
            TypeName="Samples.AspNet.VB.AggregateData">
        </asp:ObjectDataSource>
    </div>
    </form>
</body>
</html>

備註

您可以建立一個物件來匯總數個數據域值,而不是指定傳遞給 UpdateInsertDelete 方法的數個參數。 這個物件會傳遞至 方法,而不是數個參數。

系結至資料繫結控制項之 ObjectDataSource 控制項的預設行為是資料繫結控制項會為數據源中的每個參數建立 Parameter 物件。 如果商務物件有許多欄位,產生的方法也會有許多欄位。 屬性 DataObjectTypeName 可讓您指定具有每個資料欄位屬性的類型。 然後,執行時間會建立一個物件並設定其所有屬性,而不是將數個參數傳遞至 方法。 這個物件會新增至方法呼叫的參數集合。

屬性所 DataObjectTypeName 指定的型別必須具有無參數的無參數建構函式,因此 ObjectDataSource 控制項可以建立類型的實例。 此類型也必須具有可設定的屬性,讓 ObjectDataSource 控制項以從資料繫結控制項傳遞的值填入物件。 控制項上的 ObjectDataSource 屬性名稱應該完全符合資料繫結控制項所傳遞之值的參數名稱。

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

屬性 DataObjectTypeName 會委派給 DataObjectTypeNameObjectDataSource 控制項相關聯之 的 ObjectDataSourceView 屬性。

適用於

另請參閱