0

Basically, I'm asking for the best way to do the following: I have a layout like this

<div class="outer">
    <div class="inner"><!-- content --></div>
    <div class="inner"><!-- content --></div>
    <div class="inner"><!-- content --></div>
    <div class="inner"><!-- content --></div>
</div>
<div class="outer">
    <div class="inner"><!-- content --></div>
    <div class="inner"><!-- content --></div>
    <div class="inner"><!-- content --></div>
    <div class="inner"><!-- content --></div>
</div>
<div class="outer">
    <div class="inner"><!-- content --></div>
    <div class="inner"><!-- content --></div>
    <div class="inner"><!-- content --></div>
    <div class="inner"><!-- content --></div>
</div>

So, the outer div is to be repeated three times, with each inner div repeated four times. I'm doing this in ASP.net, the information is coming from a database (probably through Linq)...

I am asking is it better to use a nested repeater and spoon feed each repeater four records at a time through a for/foreach loop, or output the outer divs as literals?

EDITED

<asp:Repeater ID="MyOuterRepeater" runat="server">
    <ItemTemplate>
        <div class="outer">
        <asp:Repeater ID="MyInnerRepeater" runat="server">
            <ItemTemplate>
                <div class="inner">
                <!-- content -->
                </div>
            </ItemTemplate>
        </asp:Repeater>
        </div>
    </ItemTemplate>
</asp:Repeater>

Or just have one, with opening/ending literals at the top and bottom, making them visible only when needed?

EDIT: Does anybody have an example how it would be done^^^

cjhubbard
  • 3
  • 3
  • I think this is the cleanest way to do it. Sure there are options which are better performing, but will create a mess when you have to add another nested element within your inner div. This is neat code, scalable, maintainable. Makes you wonder if that millisecond in run-time is worth the trouble.. – Steven Ryssaert Mar 22 '11 at 11:16

1 Answers1

0

Your repeater definition will not create resulting HTML that you also provided in your question. It should actually be:

<asp:Repeater ID="MyOuterRepeater" runat="server">
    <ItemTemplate>
        <div class="outer">
            <asp:Repeater ID="MyInnerRepeater" runat="server">
                <ItemTemplate>
                    <div class="inner">
                    <!-- content -->
                    </div>
                </ItemTemplate>
            </asp:Repeater>
        </div>
    </ItemTemplate>
</asp:Repeater>

Otherwise your outer div will only be rendered once and all inner divs will be contained in the same outer div.

Nesting repeaters?

If your inner elements are dynamic depending on data, you should of course use nested repeaters, but if every outer will contain four inner, then I don't see any benefit in having nested repeaters. It will actually execute slower and consume more resources. And that's something you don't want, do you?

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404