Compartir a través de


ObjectDataSource.DataObjectTypeName Propiedad

Definición

Obtiene o establece el nombre de una clase que el control ObjectDataSource utiliza para un parámetro de una operación que actualiza, inserta o elimina datos, en lugar de pasar valores individuales desde el control enlazado a datos.

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

Valor de propiedad

String

Nombre de clase completo o parcial que identifica el tipo del objeto que ObjectDataSource puede usar como parámetro para una operación Insert(), Update() o Delete(). El valor predeterminado es una cadena vacía ("").

Ejemplos

La sección contiene dos ejemplos de código. En el primer ejemplo de código se muestra cómo implementar un tipo que combina todos los valores de parámetro en un objeto mediante la DataObjectTypeName propiedad . En el segundo ejemplo de código se muestra la página web que usa las dos clases que se usan en el primer ejemplo de código.

En el ejemplo de código siguiente se muestra cómo implementar un tipo que combina todos los valores de parámetro en un objeto mediante la DataObjectTypeName propiedad . El método select de la AggregateData clase devuelve un DataTable objeto con dos columnas denominadas Name y Number. De forma similar, la NewData clase define dos propiedades de lectura y escritura, Name y Number. El Insert método de la AggregateData clase toma un parámetro de tipo NewData. La TypeName propiedad de ObjectDataSource se establece AggregateData en y la DataObjectTypeName propiedad se establece en 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

En el ejemplo de código siguiente se muestra la página web que usa las dos clases que se usan en el ejemplo de código anterior.

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

Comentarios

En lugar de especificar varios parámetros que se pasan a los Updatemétodos , Inserty Delete , puede crear un objeto que agregue varios valores de campo de datos. Este objeto se pasa a los métodos, en lugar de a varios parámetros.

El comportamiento predeterminado de un ObjectDataSource control enlazado a un control enlazado a datos es que el control enlazado a datos crea un Parameter objeto para cada parámetro del origen de datos. Si el objeto de negocio tiene muchos campos, el método resultante también tiene muchos campos. La DataObjectTypeName propiedad permite especificar un tipo que tenga una propiedad para cada campo de datos. A continuación, en lugar de pasar varios parámetros al método , el tiempo de ejecución crea un objeto y establece todas sus propiedades. Este objeto se agrega a la colección parameters de la llamada al método .

El tipo especificado por la DataObjectTypeName propiedad debe tener un constructor sin parámetros que no tenga parámetros, por lo que el ObjectDataSource control puede crear una instancia del tipo . El tipo también debe tener propiedades configurables que permitan al control rellenar el ObjectDataSource objeto con valores que se pasan desde el control enlazado a datos. Se espera que los nombres de propiedad del ObjectDataSource control coincidan exactamente con los nombres de parámetros de los valores pasados por el control enlazado a datos.

Cuando se establece la DataObjectTypeName propiedad y el ObjectDataSource control está asociado a un control enlazado a datos, los métodos especificados por las InsertMethod propiedades y DeleteMethod deben tener un parámetro del tipo especificado en la DataObjectTypeName propiedad . Si la ConflictDetection propiedad se establece en el OverwriteChanges valor , el método especificado por la UpdateMethod propiedad debe tener un parámetro del tipo especificado en la DataObjectTypeName propiedad . Si la ConflictDetection propiedad se establece en el CompareAllValues valor , el método especificado por la UpdateMethod propiedad debe tener dos parámetros del tipo especificado en la DataObjectTypeName propiedad . El primer parámetro contiene los valores originales; el segundo parámetro contiene los nuevos valores.

La DataObjectTypeName propiedad delega a la DataObjectTypeName propiedad del ObjectDataSourceView objeto asociado al ObjectDataSource control .

Se aplica a

Consulte también