DataBinder.Eval ASP.NET Guide

DataBinder.Eval method in ASP.NET is used to retrieve the value of a property from a bound data source. It is typically used with data-binding controls like GridView, Repeater, DataList, etc.

Here is the basic syntax of the DataBinder.Eval method:

<%# DataBinder.Eval(Container.DataItem, "PropertyName") %>

Among them, Container.DataItem represents the bound data source object, and “PropertyName” represents the name of the property from which to retrieve the value.

For example, in GridView, you can bind the property values from the data source to GridView columns using the DataBinder.Eval method.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField='<%# DataBinder.Eval(Container.DataItem, "Name") %>' HeaderText="Name" />
    </Columns>
</asp:GridView>

In the code, you can also use the DataBinder.Eval method to dynamically retrieve property values from the data source, for example:

string name = DataBinder.Eval(dataItem, "Name").ToString();

The DataBinder.Eval method can help simplify the binding process between data binding controls and data sources, making the code cleaner and easier to read.

bannerAds