DataBinder.Eval DataBinder.Eval DataBinder.Eval DataBinder.Eval Method

定義

使用反映在 Run Time 時針對物件剖析並評估資料繫結運算式。Uses reflection to parse and evaluate a data-binding expression against an object at run time.

多載

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.

備註

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

Eval(Object, String) Eval(Object, String) Eval(Object, String) 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 Object Object 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 String String 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. 若要這樣做,您可以<%# %>使用運算式語法,如同標準 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 控制項(例如GridViewDetailsViewDataListRepeatercontainer ,都應該是Container.DataItemFor 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 Forms 使用模型系結的教學課程系列,請參閱模型系結和 webform。For a tutorial series on using model binding with Web Forms, see Model Binding and Web Forms.

另請參閱

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.

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 Object Object 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 String String 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 String String 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. 若要以宣告方式使用方法, <%# %>請使用運算式語法,如同標準 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 控制項(例如GridViewDetailsViewDataListRepeatercontainer ,都應該是Container.DataItemFor 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 Forms 使用模型系結的教學課程系列,請參閱模型系結和 webform。For a tutorial series on using model binding with Web Forms, see Model Binding and Web Forms.

另請參閱

適用於