Container.DataItem

R

rn5a

When I do this:

<asp:DataList ID="dlProducts" runat="server">
<HeaderTemplate>
<table border="0">
<tr>
<td>Category</td>
<td><%# Container.DataItem("Category") %></td>
</tr>
</table>
</HeaderTemplate>
</asp:DataList>

then ASP.NET generates the

Object variable or With block variable not set.

error pointing to the <%# Container.DataItem("Category") %> line in the
above code but if the offending line is placed within an ItemTemplate

<asp:DataList ID="dlProducts" runat="server">
<ItemTemplate>
<table border="0">
<tr>
<td>Category</td>
<td><%# Container.DataItem("Category") %></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>

then it works fine.

Can't <%# Container.DataItem("Category") %> be used within the
HeaderTemplate in a DataList (Category is a column in a SQL Server 2005
DB table)?

Note that I am binding the data to the DataList control using
SqlDataReader.
 
D

David Wier

That's because it's meant for the ItemTemplate - that's where the data gets
placed.
The HeaderTemplate is for headers - there is no data associated with it, in
the way you created it.
 
R

rn5a

So that means <%# Container.DataItem("Category") %> cannot be used
within the HeaderTemplate in a DataList control.....fine.......thanks
 
E

Eliyahu Goldin

The DataItem property applies only to data items. Header is not one. This
explains your error message.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top