ASP.net Repeater: Best practice for nested layout
-
28-10-2019 - |
Question
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^^^
Solution
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
div
s 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?