DataBinder Classe

Definizione

Fornisce supporto per le finestre di progettazione rad (Rapid Application Development) per generare e analizzare la sintassi delle espressioni di data binding. 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 di Product oggetti . Il Eval metodo può essere applicato con la stessa sintassi, ma non viene eseguito con la stessa rapidità.

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 usare il metodo Eval statico di overload di questa classe nella sintassi del 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 sul data binding, le espressioni e la sintassi ASP.NET, vedere Binding to Databases and Data-Binding Expressions Overview.

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 Form, vedere Associazione di modelli e Web Form.

Costruttori

Nome Descrizione
DataBinder()

Inizializza una nuova istanza della classe DataBinder.

Proprietà

Nome Descrizione
EnableCaching

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

Metodi

Nome Descrizione
Equals(Object)

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

(Ereditato da Object)
Eval(Object, String, String)

Valuta le espressioni di data binding in fase di esecuzione e formatta il risultato come stringa.

Eval(Object, String)

Valuta le espressioni di data binding in fase di esecuzione.

GetDataItem(Object, Boolean)

Recupera l'elemento di dati dichiarato di un oggetto, che indica l'esito positivo o negativo.

GetDataItem(Object)

Recupera l'elemento di dati dichiarato di un oggetto.

GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetIndexedPropertyValue(Object, String, String)

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

GetIndexedPropertyValue(Object, String)

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

GetPropertyValue(Object, String, String)

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

GetPropertyValue(Object, String)

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

GetType()

Ottiene il Type dell'istanza corrente.

(Ereditato da Object)
IsBindableType(Type)

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

MemberwiseClone()

Crea una copia superficiale del Objectcorrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Si applica a

Vedi anche