Search code examples
asp.netloopsrepeater

How can I use a FOR loop inside a REPEATER to create <TD>'s in a <TABLE>?


I have a repeater that is bound to a List(of T) object collection. It's a list of Inventory objects. Each Inventory object also contains a List(of T) which is a list of Date / Inventory Count pairs. When the repeater is creating a table, I need to create a TD for each of the Date / Inventory Counts. Since the number of Date / Inventory Counts is not set until runtime (using NumWeeks variable), I need to vary the number of TD's in my repeater. This is essentially what I want:

    <asp:Repeater ID="rptReport" runat="server">
            <ItemTemplate>
                <tr>
                    <td><%#DataBinder.Eval(Container.DataItem, "Department")%></td>
                    <td><%#DataBinder.Eval(Container.DataItem, "Description")%></td> 
                    <%  For x = 0 To NumWeeks%>

                    <td><%#DataBinder.Eval(Container.DataItem, "Values")(x).Value()%></td> 

                    <%  Next%>             
                </tr>
            </ItemTemplate>
        </asp:Repeater>

Solution

  • You need to place another repeater inside this repeater and assign the datasource to that inner repeater in the "ItemDataBound" event of the parent repeater. This should solve your issue.

    Hope this is helpful!!