DataList Question

S

Steve

Hi

Please could someone suggest why this works :-

<asp:DataList id="myDL" runat="server">
<ItemTemplate>
<%# DataBinder.Eval container.DataItem, "Filename") %>
</ItemTemplate>
</asp:DataList>

and this doesn't :-

<asp:DataList id="myDL" runat="server">
<ItemTemplate>
<%# Container.DataItem("Filename") %>
</ItemTemplate>
</asp:DataList>

I get the following error msg in vis studio with the
latter.

'System.Web.UI.WebControls.DataListItem.DataItem' denotes
a 'property' where a 'method' was expected

Thanks for sharing your knowledge
Steve
 
T

Teemu Keiski

Hi,

the first one uses late-binding, that is evaluates the expression at runtime
(DataBinder.Eval is designed to locate the property and output its value as
text). The other version uses early-bind way, so it must follow the exact
rules for the programming language (you use C#?).

To work the second way, you'd need to cast the Container.DataItem to its
actual type, which depends on what you bind the DataList to (with dataReader
System.Data.Common.DBDataRecord and with DataSet/DataTable/DataView it is
System.Data.DataRowView), and then access the property like the programming
language supposes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top