Repeater In Repeater
Last night while trying to figure out a way to categorize information in a webform, I found out a way to place a repeater inside another repeater, so you end up having one repeater displaying the categories and another displaying the subitems. This may not be the best way to do this but It did solve my problem;
<table width="100%" cellpadding=0 cellspacing=0 >
<asp:repeater id="repCategories" Runat="server">
<ItemTemplate>
<TR>
<TD class="h3" width="100%" background="<%# TemplatesPath %>images/header.gif"><%# Container.DataItem.CategoryHeader %>
</TD>
</TR>
<TR>
<TD>
<TABLE width="100%">
<asp:Repeater ID="repSubItems" Runat="server">
<ItemTemplate>
<TR>
<TD class="subhead"><A href="default.aspx?id=<%# Container.DataItem.ID %>"
target="_self"><%# Container.DataItem.Title %></A>
</TD>
</TR>
<TR>
<TD class="body">
<%# Container.DataItem.Description %>
</TD>
</TR>
</ItemTemplate>
<SeparatorTemplate>
<TR>
<TD width="100%" height="3px">
<HR color="#cbcdfe" SIZE="1">
</TD>
</TR>
</SeparatorTemplate>
</asp:Repeater>
</TABLE>
</TD>
</TR>
</ItemTemplate>
</asp:repeater></table>
Then I placed this code in the "ItemDataBound" Event of the main Repeater:
Private Sub repCategories_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles repCategories.ItemDataBound
Dim catItem As CategoryItem = CType(e.Item.DataItem, CategoryItem)
Dim repItem As System.Web.UI.WebControls.RepeaterItem = CType(e.Item, System.Web.UI.WebControls.RepeaterItem)
Dim repSubItems As New System.Web.UI.WebControls.Repeater
Dim ctrl As System.Web.UI.Control
For Each ctrl In repItem.Controls
If ctrl.GetType Is repSubItems.GetType Then
repSubItems = ctrl
repSubItems.DataSource = catItem.Items
repSubItems.DataBind()
End If
Next
End Sub
Basically what I'm doing here is binding the inner repeater to the "Items" collection of the DataItem object that is bound to the Repeater Item that just got created. In this case CategoryItem is a class that I belongs to my project and the main repeater is bound to an ArrayList of "CategoryItem" objects. Each CategoryItem object has an "Items" property which is also any ArrayList of objects.
Last Updated:Thursday, September 02, 2010By:alfero#
with your OpenID.
If you do not know what OpenID is,