DataBinder Klasa

Definicja

Zapewnia obsługę projektantów szybkiego tworzenia aplikacji (RAD) w celu generowania i analizowania składni wyrażeń powiązania danych. Klasa ta nie może być dziedziczona.

public ref class DataBinder sealed
public sealed class DataBinder
type DataBinder = class
Public NotInheritable Class DataBinder
Dziedziczenie
DataBinder

Przykłady

W poniższym przykładzie użyto metody statycznej GetPropertyValue do wypełnienia pól kontrolki Repeater przy użyciu ArrayList Product obiektu . Metodę Eval można zastosować przy użyciu tej samej składni, ale nie będzie działać tak szybko.

W tym przykładzie użyto klasy niestandardowej Product , która uwidacznia właściwość string Model i właściwość liczbową 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>

Poniższy kod jest klasą niestandardową Product . Ten kod powinien znajdować się w oddzielnym pliku klasy w katalogu App_Code, takim jak Product.cs lub 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

Uwagi

Możesz użyć przeciążonej metody statycznej Eval tej klasy w składni powiązania danych na stronie sieci Web ASP.NET. Zapewnia to łatwiejszą składnię do pracy z niż standardowym powiązaniem danych. Jednak ze względu na to, że DataBinder.Eval zapewnia automatyczną konwersję typów, może to spowodować obniżenie wydajności.

Aby uzyskać więcej informacji na temat ASP.NET powiązania danych, wyrażeń i składni, zobacz Binding to Databases and Data-Binding Expressions Overview (Powiązanie z bazami danych i wyrażeniami powiązania danych — omówienie).

Począwszy od .NET Framework 4.5, można użyć powiązania modelu, aby uprościć niektóre zadania, które trzeba było wykonać za pomocą powiązania danych we wcześniejszych wersjach. Aby zapoznać się z serią samouczków dotyczącą używania powiązania modelu z Web Forms, zobacz Powiązanie modelu i Web Forms.

Konstruktory

DataBinder()

Inicjuje nowe wystąpienie klasy DataBinder.

Właściwości

EnableCaching

Pobiera lub ustawia wartość wskazującą, czy buforowanie danych jest włączone w czasie wykonywania.

Metody

Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
Eval(Object, String)

Oblicza wyrażenia powiązania danych w czasie wykonywania.

Eval(Object, String, String)

Oblicza wyrażenia powiązania danych w czasie wykonywania i formatuje wynik jako ciąg.

GetDataItem(Object)

Pobiera zadeklarowany element danych obiektu.

GetDataItem(Object, Boolean)

Pobiera zadeklarowany element danych obiektu wskazujący powodzenie lub niepowodzenie.

GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetIndexedPropertyValue(Object, String)

Pobiera wartość właściwości określonego kontenera i ścieżki nawigacji.

GetIndexedPropertyValue(Object, String, String)

Pobiera wartość określonej właściwości dla określonego kontenera, a następnie formatuje wyniki.

GetPropertyValue(Object, String)

Pobiera wartość określonej właściwości określonego obiektu.

GetPropertyValue(Object, String, String)

Pobiera wartość określonej właściwości określonego obiektu, a następnie formatuje wyniki.

GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
IsBindableType(Type)

Określa, czy określony typ danych może być powiązany.

MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Dotyczy

Zobacz też