DataBinder 类

定义

提供对应用程序快速开发 (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
继承
DataBinder

示例

下面的示例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 或 Product .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 窗体中使用模型绑定的教程系列,请参阅模型绑定和 Web 窗体For a tutorial series on using model binding with Web Forms, see Model Binding and Web Forms.

构造函数

DataBinder()

初始化 DataBinder 类的新实例。Initializes a new instance of the DataBinder class.

属性

EnableCaching

获取或设置一个值,该值指示在运行时是否启用了数据缓存。Gets or sets a value that indicates whether data caching is enabled at run time.

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
Eval(Object, String)

在运行时计算数据绑定表达式。Evaluates data-binding expressions at run time.

Eval(Object, String, String)

在运行时计算数据绑定表达式,并将结果的格式设置为字符串。Evaluates data-binding expressions at run time and formats the result as a string.

GetDataItem(Object)

检索对象的已声明数据项。Retrieves an object's declared data item.

GetDataItem(Object, Boolean)

检索对象的已声明数据项,以指示成功或失败。Retrieves an object's declared data item, indicating success or failure.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetIndexedPropertyValue(Object, String)

检索指定的容器和导航路径的属性值。Retrieves the value of a property of the specified container and navigation path.

GetIndexedPropertyValue(Object, String, String)

检索指定容器的指定属性的值,然后设置结果的格式。Retrieves the value of the specified property for the specified container, and then formats the results.

GetPropertyValue(Object, String)

检索指定对象的指定属性的值。Retrieves the value of the specified property of the specified object.

GetPropertyValue(Object, String, String)

检索指定对象的指定属性的值,然后设置结果的格式。Retrieves the value of the specified property of the specified object, and then formats the results.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
IsBindableType(Type)

确定指定数据类型是否可绑定。Determines whether the specified data type can be bound.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅