468,257 Members | 1,414 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,257 developers. It's quick & easy.

datalist within a datalist?

Is it possible in asp.net to have a datalist in the itemtemplate of another
datalist?

For example:

<asp:datalist id="MyList" runat="server">
<ItemTemplate>
<table border="0" width="300">
<tr>
<td><%# DataBinder.Eval(Container.DataItem, "title") %></td>
</tr>
<tr>
<td><asp:datalist id="AnotherList" runat="server">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "something")
%>
</ItemTemplate>
</td>
</tr>
</table>
</ItemTemplate>
</asp:datalist>

Then in the Code behind:

MyList.DataSource = MyDatabase.GetTitles(); // MyDatabase.GetTitles returns
a dataset
MyList.DataBind();
AnotherList.DataSource = MyDatabase.GetSomething();
AnotherList.DataBind();

I keep getting a "System.NullReferenceException: Object reference not set to
an instance of an object" error when it gets to the AnotherList.DataSource
line.

So is it possible to have a Datalist in another Datalist, and if so, what's
the correct way of doing it?
Nov 17 '05 #1
1 1598
Okay, I get it now. I have to use a OnItemDataBound, then in the Code behind,
check the e.Item.ItemType == ListItemType.Item and AlternatingItem, then
DataList MyDataList = (DataList)e.Item.FindControl("MySomething");

I have to find the control because it's contained within the first datalist.
Makes sense.

"pete K" wrote:
Is it possible in asp.net to have a datalist in the itemtemplate of another
datalist?

For example:

<asp:datalist id="MyList" runat="server">
<ItemTemplate>
<table border="0" width="300">
<tr>
<td><%# DataBinder.Eval(Container.DataItem, "title") %></td>
</tr>
<tr>
<td><asp:datalist id="AnotherList" runat="server">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "something")
%>
</ItemTemplate>
</td>
</tr>
</table>
</ItemTemplate>
</asp:datalist>

Then in the Code behind:

MyList.DataSource = MyDatabase.GetTitles(); // MyDatabase.GetTitles returns
a dataset
MyList.DataBind();
AnotherList.DataSource = MyDatabase.GetSomething();
AnotherList.DataBind();

I keep getting a "System.NullReferenceException: Object reference not set to
an instance of an object" error when it gets to the AnotherList.DataSource
line.

So is it possible to have a Datalist in another Datalist, and if so, what's
the correct way of doing it?

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by bill yeager | last post: by
9 posts views Thread by jjack100 | last post: by
5 posts views Thread by Petr SIMUNEK | last post: by
2 posts views Thread by rn5a | last post: by
4 posts views Thread by Peter | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.