Error using Container.DataItem("<field>") in Repeater ItemTemplate

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi. I have a straight-forward "OleDbDataReader" object that's been returned
from a SELECT statement against an MS Access file. I've tested the DataReader
and it has data and is set up right.

Now I'm trying to use the ASP.NET Repeater WebControl to display the data. I
read the documentation and it says that in the <ItemTemplate> section of the
Repeater, I can use the following notation:

<ItemTemplate>
<%# Container.DataItem("LastName") %>
</ItemTemplate>

BUT when I do this, I get the following error:

"CS0118: 'System.Web.UI.WebControls.RepeaterItem.DataItem' denotes a
'property' where a 'method' was expected"

What does this mean? If I replace this notation with:
<%# DataBinder.Eval(Container.DataItem, "ID") %>

it works, but I've read that that's FAR less efficient. What am I doing wrong?

Thanks.

Alex
 
Hi,

Container is the object reference against which the entire expression is
evaluated. Dataitem is the property of the Container object to which the
Listitem is bound. But the <ItemTemplate> tab for the listitem is evaluated
each time for the number of rows, so a simple property can't be bound to a
control expecting complex datasource.

And yes, DataBinder.Eval is slow becauses it uses reflection at runtime to
format and bind the data.

HTH
Regards
Joyjit
 
did you try explicitly casting using DataRowView ? i.e.,
((DataRowView)Container.DataItem)["LastName"]

Regards,
venkat.Murthy
 
Back
Top