DataBinder 類別

定義

支援快速應用程式開發 (RAD) 設計工具產生和剖析資料繫結運算式語法。 此類別無法獲得繼承。

public ref class DataBinder sealed
public sealed class DataBinder
type DataBinder = class
Public NotInheritable Class DataBinder
繼承
DataBinder

範例

下列範例會使用靜態 GetPropertyValue 方法,使用 ArrayListProduct 物件的 來填入 控制項的 Repeater 欄位。 方法 Eval 可以使用相同的語法來套用,但不會儘快執行。

這個範例會使用自訂 Product 類別來公開字串 Model 屬性和數值 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>

下列程式碼是自訂 Product 類別。 此程式碼應該包含在App_Code目錄中的個別類別檔案中,例如 Product.cs 或 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

備註

您可以在 ASP.NET 網頁的資料系結語法中使用這個類別的多載靜態 Eval 方法。 這提供比標準資料系結更容易處理的語法。 不過,因為 DataBinder.Eval 提供自動類型轉換,所以可能會導致效能變慢。

如需 ASP.NET 資料系結、運算式和語法的詳細資訊,請參閱 系結至資料庫 和資料 系結運算式概觀

從 .NET Framework 4.5 開始,您可以使用模型系結來簡化一些您必須透過舊版資料系結執行的工作。 如需搭配Web Form使用模型系結的教學課程系列,請參閱模型系結和Web Form

建構函式

DataBinder()

初始化 DataBinder 類別的新執行個體。

屬性

EnableCaching

取得或設定值,這個值指出是否在執行階段啟用資料快取。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
Eval(Object, String)

在執行階段評估資料繫結運算式。

Eval(Object, String, String)

在執行階段評估資料繫結運算式,並將結果格式化為字串。

GetDataItem(Object)

擷取物件的已宣告資料項目。

GetDataItem(Object, Boolean)

擷取物件的已宣告資料項目,指出其為成功或失敗。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetIndexedPropertyValue(Object, String)

擷取指定之容器和巡覽路徑的屬性值。

GetIndexedPropertyValue(Object, String, String)

擷取指定之容器的指定屬性值,然後格式化結果。

GetPropertyValue(Object, String)

擷取指定物件的指定屬性值。

GetPropertyValue(Object, String, String)

擷取指定之物件的指定屬性值,然後格式化結果。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsBindableType(Type)

判斷是否可以繫結指定的資料類型。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱