DataBinder.Eval 方法

定义

在运行时使用反射来分析和计算对象的数据绑定表达式。Uses reflection to parse and evaluate a data-binding expression against an object at run time.

重载

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.

注解

从 .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.

Eval(Object, String)

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

public:
 static System::Object ^ Eval(System::Object ^ container, System::String ^ expression);
public static object Eval (object container, string expression);
static member Eval : obj * string -> obj
Public Shared Function Eval (container As Object, expression As String) As Object

参数

container
Object

表达式根据其进行计算的对象引用。The object reference against which the expression is evaluated. 此标识符必须是以页的指定语言表示的有效对象标识符。This must be a valid object identifier in the page's specified language.

expression
String

container 对象到要放置在绑定控件属性中的公共属性值的导航路径。The navigation path from the container object to the public property value to be placed in the bound control property. 此路径必须是以点分隔的属性或字段名称字符串,如 C# 中的 Tables[0].DefaultView.[0].Price 或 Visual Basic 中的 Tables(0).DefaultView.(0).PriceThis must be a string of property or field names separated by periods, such as Tables[0].DefaultView.[0].Price in C# or Tables(0).DefaultView.(0).Price in Visual Basic.

返回

Object 实例,它是数据绑定表达式的计算结果。An Object instance that results from the evaluation of the data-binding expression.

异常

expressionnull 或修整后变成空字符串。expression is null or is an empty string after trimming.

示例

下面的示例演示如何使用Eval方法将数据绑定到Repeater控件。The following examples show how to use the Eval method to bind data to Repeater control. 它需要一个名为 "Product" 的数据类。It requires a data class named Product.

public class Product
{
    public int ProductID { get; set; }
    public string Name { get; set; }
    public double Price { get; set; }
}
Public Class Product
    Public Property ProductID As Integer
    Public Property Name As String
    Public Property Price As Double
End Class

代码隐藏文件可加载测试数据并将这些数据绑定到Repeater控件。The code-behind file loads test data and binds that data to a Repeater control.

public partial class ShowProducts : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var products = new List<Product>();
        products.Add(new Product() { ProductID = 1, Name = "Bike", Price = 150.00 });
        products.Add(new Product() { ProductID = 2, Name = "Helmet", Price = 19.99 });
        products.Add(new Product() { ProductID = 3, Name = "Tire", Price = 10.00 });

        ProductList.DataSource = products;
        ProductList.DataBind();
    }
}
Public Class ShowProducts
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim products As New List(Of Product)()
        products.Add(New Product With {.ProductID = 1, .Name = "Bike", .Price = 150.0})
        products.Add(New Product With {.ProductID = 2, .Name = "Helmet", .Price = 19.99})
        products.Add(New Product With {.ProductID = 3, .Name = "Tire", .Price = 10.0})

        ProductList.DataSource = products
        ProductList.DataBind()
    End Sub

End Class

Repeater控件的声明性语法中, Eval将方法Container.DataItem container用于参数。In the declarative syntax for the Repeater control, you use the Eval method with Container.DataItem for the container parameter.

<asp:Repeater ID="ProductList" runat="server">
    <ItemTemplate>
        <%# DataBinder.Eval(Container.DataItem, "Name") %> for only <%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}") %>
        <br />
        <a href='<%# DataBinder.Eval(Container.DataItem, "ProductID", "details.asp?id={0}") %>'>See Details</a>
        <br />
        <br />
    </ItemTemplate>
</asp:Repeater>
<asp:Repeater ID="ProductList" runat="server">
    <ItemTemplate>
        <%# DataBinder.Eval(Container.DataItem, "Name") %> for only <%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}") %>
        <br />
        <a href='<%# DataBinder.Eval(Container.DataItem, "ProductID", "details.asp?id={0}") %>'>See Details</a>
        <br />
        <br />
    </ItemTemplate>
</asp:Repeater>

或者,你可以调用Eval函数,但不container包括参数。Or, you can call Eval function and not include the container parameter.

<asp:Repeater ID="ProductList" runat="server">
    <ItemTemplate>
        <%# Eval("Name") %> for only <%# Eval("Price", "{0:c}") %>
        <br />
        <a href='<%# Eval("ProductID", "details.asp?id={0}") %>'>See Details</a>
        <br />
        <br />
    </ItemTemplate>
</asp:Repeater>
<asp:Repeater ID="ProductList" runat="server">
    <ItemTemplate>
        <%# Eval("Name") %> for only <%# Eval("Price", "{0:c}") %>
        <br />
        <a href='<%# Eval("ProductID", "details.asp?id={0}") %>'>See Details</a>
        <br />
        <br />
    </ItemTemplate>
</asp:Repeater>

注解

expression参数的值的计算结果必须为公共属性。The value of the expression parameter must evaluate to a public property.

在快速应用程序开发(RAD)设计器(如 Visual Studio)中创建数据绑定时,将自动调用此方法。This method is automatically called when you create data bindings in a rapid application development (RAD) designer such as Visual Studio. 还可以通过声明方式使用它来简化到文本字符串的转换。You can also use it declaratively to simplify casting to a text string. 为此,请使用<%# %> expression 语法,如标准 ASP.NET 数据绑定中所用。To do so, you use the <%# %> expression syntax, as used in standard ASP.NET data binding.

在将数据绑定到模板列表中的控件时,此方法特别有用。This method is particularly useful when binding data to controls that are in a templated list.

备注

由于此方法使用反射在运行时执行后期绑定计算,因此与标准 ASP.NET 数据绑定语法相比,它可能会导致性能明显降低。Because this method performs late-bound evaluation, using reflection at run time, it can cause performance to noticeably slow compared to standard ASP.NET data-binding syntax.

对于任何列表 Web 控件(如GridView DataListDetailsView、或Repeatercontainer ,都应Container.DataItem为。For any of the list Web controls, such as GridView, DetailsView, DataList, or Repeater, container should be Container.DataItem. 如果要针对页面进行绑定, container则应为。 PageIf you are binding against the page, container should be Page.

从 .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.

另请参阅

Eval(Object, String, String)

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

public:
 static System::String ^ Eval(System::Object ^ container, System::String ^ expression, System::String ^ format);
public static string Eval (object container, string expression, string format);
static member Eval : obj * string * string -> string
Public Shared Function Eval (container As Object, expression As String, format As String) As String

参数

container
Object

表达式根据其进行计算的对象引用。The object reference against which the expression is evaluated. 此标识符必须是以页的指定语言表示的有效对象标识符。This must be a valid object identifier in the page's specified language.

expression
String

container 对象到要放置在绑定控件属性中的公共属性值的导航路径。The navigation path from the container object to the public property value to be placed in the bound control property. 此路径必须是以点分隔的属性或字段名称字符串,如 C# 中的 Tables[0].DefaultView.[0].Price 或 Visual Basic 中的 Tables(0).DefaultView.(0).PriceThis must be a string of property or field names separated by periods, such as Tables[0].DefaultView.[0].Price in C# or Tables(0).DefaultView.(0).Price in Visual Basic.

format
String

.NET Framework 格式字符串(如 Format(String, Object) 使用的字符串),它将数据绑定表达式返回的 Object 实例转换为 String 对象。A .NET Framework format string (like those used by Format(String, Object)) that converts the Object instance returned by the data-binding expression to a String object.

返回

String 对象,它是计算数据绑定表达式和向字符串类型转换的结果。A String object that results from evaluating the data-binding expression and converting it to a string type.

示例

下面的示例演示如何使用Eval方法将数据绑定到Repeater控件。The following examples show how to use the Eval method to bind data to Repeater control. 它需要一个名为 "Product" 的数据类。It requires a data class named Product.

public class Product
{
    public int ProductID { get; set; }
    public string Name { get; set; }
    public double Price { get; set; }
}
Public Class Product
    Public Property ProductID As Integer
    Public Property Name As String
    Public Property Price As Double
End Class

代码隐藏文件可加载测试数据并将这些数据绑定到Repeater控件。The code-behind file loads test data and binds that data to a Repeater control.

public partial class ShowProducts : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var products = new List<Product>();
        products.Add(new Product() { ProductID = 1, Name = "Bike", Price = 150.00 });
        products.Add(new Product() { ProductID = 2, Name = "Helmet", Price = 19.99 });
        products.Add(new Product() { ProductID = 3, Name = "Tire", Price = 10.00 });

        ProductList.DataSource = products;
        ProductList.DataBind();
    }
}
Public Class ShowProducts
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim products As New List(Of Product)()
        products.Add(New Product With {.ProductID = 1, .Name = "Bike", .Price = 150.0})
        products.Add(New Product With {.ProductID = 2, .Name = "Helmet", .Price = 19.99})
        products.Add(New Product With {.ProductID = 3, .Name = "Tire", .Price = 10.0})

        ProductList.DataSource = products
        ProductList.DataBind()
    End Sub

End Class

Repeater控件的声明性语法中, Eval将方法Container.DataItem container用于参数。In the declarative syntax for the Repeater control, you use the Eval method with Container.DataItem for the container parameter.

<asp:Repeater ID="ProductList" runat="server">
    <ItemTemplate>
        <%# DataBinder.Eval(Container.DataItem, "Name") %> for only <%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}") %>
        <br />
        <a href='<%# DataBinder.Eval(Container.DataItem, "ProductID", "details.asp?id={0}") %>'>See Details</a>
        <br />
        <br />
    </ItemTemplate>
</asp:Repeater>
<asp:Repeater ID="ProductList" runat="server">
    <ItemTemplate>
        <%# DataBinder.Eval(Container.DataItem, "Name") %> for only <%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}") %>
        <br />
        <a href='<%# DataBinder.Eval(Container.DataItem, "ProductID", "details.asp?id={0}") %>'>See Details</a>
        <br />
        <br />
    </ItemTemplate>
</asp:Repeater>

或者,你可以调用Eval函数,但不container包括参数。Or, you can call Eval function and not include the container parameter.

<asp:Repeater ID="ProductList" runat="server">
    <ItemTemplate>
        <%# Eval("Name") %> for only <%# Eval("Price", "{0:c}") %>
        <br />
        <a href='<%# Eval("ProductID", "details.asp?id={0}") %>'>See Details</a>
        <br />
        <br />
    </ItemTemplate>
</asp:Repeater>
<asp:Repeater ID="ProductList" runat="server">
    <ItemTemplate>
        <%# Eval("Name") %> for only <%# Eval("Price", "{0:c}") %>
        <br />
        <a href='<%# Eval("ProductID", "details.asp?id={0}") %>'>See Details</a>
        <br />
        <br />
    </ItemTemplate>
</asp:Repeater>

注解

expression值必须计算为公共属性。The value of expression must evaluate to a public property.

有关 .NET Framework 中的格式字符串的详细信息,请参阅格式设置类型For more information about format strings in the .NET Framework, see Formatting Types.

在快速应用程序开发(RAD)设计器(如 Visual Studio)中创建数据绑定时,将自动调用此方法。This method is automatically called when you create data bindings in a rapid application development (RAD) designer such as Visual Studio. 还可以通过声明方式使用它将数据Object绑定表达式的结果转换String为。You can also use it declaratively to convert the Object resulting from the data-binding expression to a String. 若要以声明方式使用方法, <%# %>请使用 expression 语法,如标准 ASP.NET 数据绑定中所用。To use the method declaratively, use the <%# %> expression syntax, as used in standard ASP.NET data binding.

在将数据绑定到模板列表中的控件时,此方法特别有用。This method is particularly useful when binding data to controls that are in a templated list.

备注

由于此方法使用反射在运行时执行后期绑定计算,因此与标准 ASP.NET 数据绑定语法相比,它可能会导致性能明显降低。Because this method performs late-bound evaluation, using reflection at run time, it can cause performance to noticeably slow compared to standard ASP.NET data-binding syntax. 慎用此方法,特别是在不需要字符串格式设置时。Use this method judiciously, particularly when string formatting is not required.

对于任何列表 Web 控件(如GridView DataListDetailsView、或Repeatercontainer ,都应Container.DataItem为。For any of the list Web controls, such as GridView, DetailsView, DataList, or Repeater, container should be Container.DataItem. 如果要针对页面进行绑定, container则应为。 PageIf you are binding against the page, container should be Page.

从 .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.

另请参阅

适用于