DataBinder Classe

Definizione

Fornisce il supporto per la generazione e l'analisi della sintassi di espressioni di associazione dati da parte di strumenti per lo sviluppo rapido di applicazioni (RAD, Rapid Application Development). La classe non può essere ereditata.

public ref class DataBinder sealed
public sealed class DataBinder
type DataBinder = class
Public NotInheritable Class DataBinder
Ereditarietà
DataBinder

Esempio

Nell'esempio seguente viene utilizzato il metodo statico GetPropertyValue per popolare i campi di un Repeater controllo utilizzando un ArrayList oggetto di Product oggetti . Il Eval metodo può essere applicato con la stessa sintassi, ma non viene eseguito rapidamente.

In questo esempio viene utilizzata una classe personalizzata Product che espone una proprietà stringa Model e una proprietà numerica UnitPrice .

<%@ Page Language="C#" %>
<%@ Import Namespace="ASPSample" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void  Page_Load(object sender, EventArgs e)
{
        // Create and populate an ArrayList to store the products.
        ArrayList ProductList = new ArrayList();
        ProductList.Add(new Product("Standard", 99.95));
        ProductList.Add(new Product("Deluxe", 159.95));

        // Bind the array list to Repeater
        ListRepeater.DataSource = ProductList;
        ListRepeater.DataBind();
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>DataBinder Example</title>
</head>
<body>
<form id="Form2" runat="server">
<table>
<asp:Repeater id="ListRepeater" runat="server">
    <HeaderTemplate>
    <tr>
        <th style="width:50; text-align:left">Model</th>
        <th style="width:100; text-align:right">Unit Price</th>
    </tr>
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
        <!-- Databind to the Product information using the DataBinder methods. 
             The Container.DataItem refers to the ArrayList object bound to 
             the ASP:Repeater in the Page Load event. -->
        <td>
            <%#DataBinder.GetPropertyValue(Container.DataItem, "Model")%>
        </td>
        <!-- Format the UnitPrice as currency. ({0:c}) -->
        <td style="text-align:right">
            <%#DataBinder.GetPropertyValue(Container.DataItem,
                         "UnitPrice", "{0:c}")%>
        </td>
    </tr>
    </ItemTemplate>
</asp:Repeater>
</table>
</form>
</body>
</html>
<%@ Page Language="vb" %>
<%@ Import Namespace="ASPSample" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    Private Sub Page_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load

        ' Create and populate an ArrayList to store the products.
        Dim ProductList As New ArrayList
        ProductList.Add(New Product("Standard", 99.95))
        ProductList.Add(New Product("Deluxe", 159.95))

        ' Bind the array list to Repeater
        ListRepeater.DataSource = ProductList
        ListRepeater.DataBind()

    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>DataBinder Example</title>
</head>
<body>
<form id="Form2" runat="server">
<table>
<asp:Repeater id="ListRepeater" runat="server">
    <HeaderTemplate>
    <tr>
        <th style="width:50; text-align:left">Model</th>
        <th style="width:100; text-align:right">Unit Price</th>
    </tr>
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
        <!-- Databind to the Product information using the DataBinder methods. 
             The Container.DataItem refers to the ArrayList object bound to 
             the ASP:Repeater in the Page Load event. -->
        <td>
            <%#DataBinder.GetPropertyValue(Container.DataItem, "Model")%>
        </td>
        <!-- Format the UnitPrice as currency. ({0:c}) -->
        <td style="text-align:right">
            <%#DataBinder.GetPropertyValue(Container.DataItem, _
                         "UnitPrice", "{0:c}")%>
        </td>
    </tr>
    </ItemTemplate>
</asp:Repeater>
</table>
</form>
</body>
</html>

Il codice seguente è la classe personalizzata Product . Questo codice deve essere incluso in un file di classe separato nella directory App_Code, ad esempio Product.cs o Product.vb.

namespace ASPSample
{

    public class Product
    {
        string _Model;
        double _UnitPrice;

        public Product(string Model, double UnitPrice)
        {
            _Model = Model;
            _UnitPrice = UnitPrice;
        }

        // The product Model.
        public string Model
        {
            get {return _Model;}
            set {_Model = value;}
        }
            
        // The price of the each product.
        public double UnitPrice
        {
            get {return _UnitPrice;}
            set {_UnitPrice = value;}
        }
    }
}
Namespace ASPSample

    Public Class Product
        Private _Model As String
        Private _UnitPrice As Double

        ' The product Model.
        Public Property Model() As String
            Get
                Return _Model
            End Get
            Set(ByVal Value As String)
                _Model = Value
            End Set
        End Property

        ' The price of the each product.
        Public Property UnitPrice() As Double
            Get
                Return _UnitPrice
            End Get
            Set(ByVal Value As Double)
                _UnitPrice = Value
            End Set
        End Property


        Public Sub New(ByVal Model As String, ByVal UnitPrice As Double)
            _Model = Model
            _UnitPrice = UnitPrice
        End Sub

    End Class

End Namespace

Commenti

È possibile utilizzare il metodo statico Eval di overload di questa classe nella sintassi di data binding in una pagina Web ASP.NET. In questo modo è possibile usare una sintassi più semplice rispetto al data binding standard. Tuttavia, poiché DataBinder.Eval fornisce la conversione automatica dei tipi, può comportare prestazioni più lente.

Per altre informazioni su ASP.NET data binding, espressioni e sintassi, vedere Cenni preliminarisull'associazione a database e espressioni di data binding.

A partire da .NET Framework 4.5, è possibile usare l'associazione di modelli per semplificare alcune delle attività da eseguire tramite il data binding nelle versioni precedenti. Per una serie di esercitazioni sull'uso dell'associazione di modelli con Web Forms, vedere Associazione di modelli e Web Forms.

Costruttori

DataBinder()

Inizializza una nuova istanza della classe DataBinder.

Proprietà

EnableCaching

Ottiene o imposta un valore che indica se la memorizzazione di dati nella cache è attivata in fase di esecuzione.

Metodi

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
Eval(Object, String)

Valuta le espressioni di associazione dati in fase di esecuzione.

Eval(Object, String, String)

Valuta le espressioni di associazione dati in fase di esecuzione e formatta il risultato come stringa.

GetDataItem(Object)

Recupera l'elemento di dati dichiarato di un oggetto.

GetDataItem(Object, Boolean)

Recupera l'elemento di dati dichiarato di un oggetto indicando l'esito positivo o negativo.

GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetIndexedPropertyValue(Object, String)

Recupera il valore di una proprietà del contenitore e del percorso di navigazione specificati.

GetIndexedPropertyValue(Object, String, String)

Recupera il valore della proprietà specificata per il contenitore specificato e quindi formatta i risultati.

GetPropertyValue(Object, String)

Recupera il valore della proprietà specificata dell'oggetto specificato.

GetPropertyValue(Object, String, String)

Recupera il valore della proprietà specificata dell'oggetto specificato e quindi formatta i risultati.

GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
IsBindableType(Type)

Determina se il tipo di dati specificato può essere associato.

MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Si applica a

Vedi anche