What does <%# DataBinder.Eval(Container.DataItem,"ColumnName") %> in the Item Template do exactly?

Ananth picture Ananth · Jan 27, 2011 · Viewed 58.2k times · Source

Iam using DataList for the first time. Every thing works fine and I am able to see the data in the screen. I am making use of this code in the item template.

<asp:DataList ID="DataList1" runat="server">
    <FooterTemplate>          
    </FooterTemplate>
    <HeaderTemplate>              
    </HeaderTemplate>
    <ItemTemplate>          
        <%# DataBinder.Eval(Container.DataItem,"AA") %>
        <%# DataBinder.Eval(Container.DataItem,"BB") %>
        <%# DataBinder.Eval(Container.DataItem,"CC") %>
    </ItemTemplate>
</asp:DataList>

This is the DataTable that I am binding

DataTable dt = new DataTable();
dt.Columns.Add("AA");
dt.Columns.Add("BB");
dt.Columns.Add("CC");

dt.Rows.Add("1", "2", "3");
dt.Rows.Add("10", "20", "30");
dt.Rows.Add("100", "200", "300");
dt.Rows.Add("1000", "2000", "3000");

DataList1.DataSource = dt;
DataList1.DataBind();

What does DataBinder.Eval(Container.DataItem,"ColumnName") do exactly.? Thank you in Advance

Answer

Vijay Sirigiri picture Vijay Sirigiri · Jan 27, 2011

Argument 1: Container.DataItem refers to the datasource that is bound to the current container.

Argument 2: The public property on the DataItem which should be evaluated.

So Eval uses reflection to evaluate the public property on the DataItem.

ex:

In you case it evaluates the BB column on the DataTable.