DataBinder 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为快速应用程序开发(RAD)设计器提供支持,以生成和分析数据绑定表达式语法。 此类不能被继承。
public ref class DataBinder sealed
public sealed class DataBinder
type DataBinder = class
Public NotInheritable Class DataBinder
- 继承
-
DataBinder
示例
以下示例使用静态GetPropertyValue方法使用对象填充控件Repeater的ArrayList字段Product。
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 数据绑定、表达式和语法的详细信息,请参阅 Binding to Databases 和 Data-Binding 表达式概述。
从 .NET Framework 4.5 开始,可以使用模型绑定来简化在早期版本中通过数据绑定执行的一些任务。 有关将模型绑定与 Web 窗体配合使用的教程系列,请参阅 模型绑定和 Web 窗体。
构造函数
| 名称 | 说明 |
|---|---|
| DataBinder() |
初始化 DataBinder 类的新实例。 |
属性
| 名称 | 说明 |
|---|---|
| EnableCaching |
获取或设置一个值,该值指示是否在运行时启用数据缓存。 |
方法
| 名称 | 说明 |
|---|---|
| Equals(Object) |
确定指定的对象是否等于当前对象。 (继承自 Object) |
| Eval(Object, String, String) |
在运行时计算数据绑定表达式,并将结果格式化为字符串。 |
| Eval(Object, String) |
在运行时计算数据绑定表达式。 |
| GetDataItem(Object, Boolean) |
检索对象的声明数据项,指示成功或失败。 |
| GetDataItem(Object) |
检索对象的声明数据项。 |
| GetHashCode() |
用作默认哈希函数。 (继承自 Object) |
| GetIndexedPropertyValue(Object, String, String) |
检索指定容器的指定属性的值,然后设置结果的格式。 |
| GetIndexedPropertyValue(Object, String) |
检索指定容器和导航路径的属性的值。 |
| GetPropertyValue(Object, String, String) |
检索指定对象的指定属性的值,然后设置结果的格式。 |
| GetPropertyValue(Object, String) |
检索指定对象的指定属性的值。 |
| GetType() |
获取当前实例的 Type。 (继承自 Object) |
| IsBindableType(Type) |
确定是否可以绑定指定的数据类型。 |
| MemberwiseClone() |
创建当前 Object的浅表副本。 (继承自 Object) |
| ToString() |
返回一个表示当前对象的字符串。 (继承自 Object) |