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

属性值

部分或完全限定类名,用于标识可由 ObjectDataSource 用作 Insert()Update()Delete() 操作的参数的对象类型。 默认值为空字符串("")。

示例

部分包含两个代码示例。 第一个代码示例演示如何使用 DataObjectTypeName 属性实现将所有参数值合并到一个对象中的类型。 第二个代码示例演示使用第一个代码示例中使用的两个类的网页。

下面的代码示例演示如何使用 DataObjectTypeName 属性实现将所有参数值合并到一个对象中的类型。 类的 AggregateData select 方法返回一个对象,DataTable该对象包含名为 和 Number的两列Name。 同样, NewData 类定义两个读/写属性: NameNumberInsert类的 AggregateData 方法采用类型的NewData一个参数。 TypeNameObjectDataSource 属性设置为 AggregateDataDataObjectTypeName属性设置为 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与数据绑定控件相关联时,由 InsertMethodDeleteMethod 属性指定的方法必须具有属性中指定的DataObjectTypeName类型的一个参数。 如果属性 ConflictDetection 设置为 OverwriteChanges 值,则属性 UpdateMethod 指定的方法必须具有属性中指定的 DataObjectTypeName 类型的一个参数。 如果属性 ConflictDetection 设置为 CompareAllValues 值,则由 UpdateMethod 属性指定的方法必须具有属性中指定的 DataObjectTypeName 类型的两个参数。 第一个参数包含原始值;第二个参数包含新值。

属性DataObjectTypeName委托给DataObjectTypeName与控件关联的 ObjectDataSourceObjectDataSourceView 属性。

适用于

另请参阅