DataBinder DataBinder DataBinder DataBinder Class

定義

支援快速應用程式開發 (RAD) 設計工具產生和剖析資料繫結運算式語法。Provides support for rapid application development (RAD) designers to generate and parse data-binding expression syntax. 這個類別無法被繼承。This class cannot be inherited.

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

範例

下列範例會GetPropertyValue使用靜態方法,以使用ArrayList物件的Product來填入Repeater控制項的欄位。The following example uses the static GetPropertyValue method to populate the fields of a Repeater control using an ArrayList of Product objects. Eval方法可以使用相同的語法來套用,但它不會快速地執行。The Eval method could be applied with the same syntax, but it would not perform as quickly.

這個範例會使用公開Product字串Model屬性和數值UnitPrice屬性的自訂類別。This example uses a custom Product class which exposes a string Model property and a numeric UnitPrice property.

<%@ 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類別。The following code is the custom Product class. 這個程式碼應該包含在 App_Code 目錄中的個別類別檔案中,例如 Product.cs 或 .vb。This code should be included in a separate class file in the App_Code directory, such as Product.cs or 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資料系結語法中,使用此類別的多載靜態方法。You can use the overloaded static Eval method of this class in data-binding syntax in an ASP.NET Web page. 這可提供比標準資料系結更容易使用的語法。This provides an easier syntax to work with than standard data binding. 不過,由於DataBinder.Eval會提供自動類型轉換,因此可能會導致效能變慢。However, because DataBinder.Eval provides automatic type conversion, it can result in slower performance.

如需有關 ASP.NET 資料系結、運算式和語法的詳細資訊,請參閱系結至資料庫和資料系結運算式總覽For more information about ASP.NET data binding, expressions, and syntax, see Binding to Databases and Data-Binding Expressions Overview.

從 .NET Framework 4.5 開始,您可以使用模型系結來簡化您必須透過較舊版本中的資料系結來執行的一些工作。Starting in .NET Framework 4.5, you can use model binding to simplify some of the tasks that you had to perform through data-binding in earlier versions. 如需搭配 Web Forms 使用模型系結的教學課程系列,請參閱模型系結和 webform。For a tutorial series on using model binding with Web Forms, see Model Binding and Web Forms.

建構函式

DataBinder() DataBinder() DataBinder() DataBinder()

初始化 DataBinder 類別的新執行個體。Initializes a new instance of the DataBinder class.

屬性

EnableCaching EnableCaching EnableCaching EnableCaching

取得或設定值,這個值指出是否在執行階段啟用資料快取。Gets or sets a value that indicates whether data caching is enabled at run time.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
Eval(Object, String) Eval(Object, String) Eval(Object, String) Eval(Object, String)

在執行階段評估資料繫結運算式。Evaluates data-binding expressions at run time.

Eval(Object, String, String) Eval(Object, String, String) Eval(Object, String, String) Eval(Object, String, String)

在執行階段評估資料繫結運算式,並將結果格式化為字串。Evaluates data-binding expressions at run time and formats the result as a string.

GetDataItem(Object) GetDataItem(Object) GetDataItem(Object) GetDataItem(Object)

擷取物件的已宣告資料項目。Retrieves an object's declared data item.

GetDataItem(Object, Boolean) GetDataItem(Object, Boolean) GetDataItem(Object, Boolean) GetDataItem(Object, Boolean)

擷取物件的已宣告資料項目,指出其為成功或失敗。Retrieves an object's declared data item, indicating success or failure.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetIndexedPropertyValue(Object, String) GetIndexedPropertyValue(Object, String) GetIndexedPropertyValue(Object, String) GetIndexedPropertyValue(Object, String)

擷取指定之容器和巡覽路徑的屬性值。Retrieves the value of a property of the specified container and navigation path.

GetIndexedPropertyValue(Object, String, String) GetIndexedPropertyValue(Object, String, String) GetIndexedPropertyValue(Object, String, String) GetIndexedPropertyValue(Object, String, String)

擷取指定之容器的指定屬性值,然後格式化結果。Retrieves the value of the specified property for the specified container, and then formats the results.

GetPropertyValue(Object, String) GetPropertyValue(Object, String) GetPropertyValue(Object, String) GetPropertyValue(Object, String)

擷取指定物件的指定屬性值。Retrieves the value of the specified property of the specified object.

GetPropertyValue(Object, String, String) GetPropertyValue(Object, String, String) GetPropertyValue(Object, String, String) GetPropertyValue(Object, String, String)

擷取指定之物件的指定屬性值,然後格式化結果。Retrieves the value of the specified property of the specified object, and then formats the results.

GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
IsBindableType(Type) IsBindableType(Type) IsBindableType(Type) IsBindableType(Type)

判斷是否可以繫結指定的資料類型。Determines whether the specified data type can be bound.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

適用於

另請參閱