I would definitely avoid the second approach - you don't want to hit the database every time you bind the parent element. As DOK says, try and organize your system properly. For me, this would mean filling out a collection of business objects and linking to it. I am doing something similar with a custom menu control (note that these are three datalists, but you can use repeaters):
On the aspx page:
<asp:DataList ID="dlMenuOne" runat="server" onitemdatabound="dlMenu_ItemDataBound" >
<ItemTemplate>
//your object
<asp:DataList ID="dlMenuTwo" runat="server" onitemdatabound="dlMenuTwo_ItemDataBound">
<ItemTemplate>
//your object child items
<asp:DataList ID="dlMenuThree" runat="server">
<ItemTemplate>
//child item child items
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
then in the code behind:
protected void dlMenu_ItemDataBound(object sender, DataListItemEventArgs e)
{
DataListItem parentList = e.Item;
DataList dlMenuTwo = (DataList)parentList.FindControl("dlMenuTwo");
MenuItem item = (MenuItem)parentList.DataItem;
dlMenuTwo.DataSource = _menu.GetChildItems(item);
dlMenuTwo.DataBind();
}
this method basically gets a reference to the associated object (parentList.DataItem) and then binds the nested DataList to the child elements (_menu.GetChildItems (item))
flesh source
share