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

60,028

Solution 1

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.

Solution 2

The following lines will be executed as many times as the number of rows in the Table.

<%# DataBinder.Eval(Container.DataItem,"AA") %>
<%# DataBinder.Eval(Container.DataItem,"BB") %>
<%# DataBinder.Eval(Container.DataItem,"CC") %>

Each time Container.DataItem will have the corresponding DataRowView of the rows in the datatable.

What happens in the item is similar to this code.

DataView dataView = new DataView(dt);
foreach (DataRowView dataRow in dataView)
{              
    System.Diagnostics.Debug.WriteLine(DataBinder.Eval(dataRow,"AA").ToString());
    System.Diagnostics.Debug.WriteLine(DataBinder.Eval(dataRow,"BB").ToString());
    System.Diagnostics.Debug.WriteLine(DataBinder.Eval(dataRow,"CC").ToString());
}

And the output obtained will be

1 2 3 10 20 30 100 200 300 1000 2000 3000

Share:
60,028
Ananth
Author by

Ananth

a programmer by choice

Updated on June 14, 2020

Comments

  • Ananth
    Ananth almost 4 years

    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