Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 795
Ananth Avatar asked Jan 27 '11 07:01

Ananth


2 Answers

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.

like image 71
Vijay Sirigiri Avatar answered Sep 27 '22 20:09

Vijay Sirigiri


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

like image 44
2 revs, 2 users 80% Avatar answered Sep 27 '22 20:09

2 revs, 2 users 80%