datalist within a datalist?

G

Guest

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?
 
G

Guest

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.
 

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

Top