Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Two Approaches to Avoiding Repeating an Item Template

Two Approaches to Avoiding Repeating an Item Template

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharphtmlasp-netwpfwcf
1 Posts 1 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AspDotNetDev
    wrote on last edited by
    #1

    An ex-cow-orker needed to create two sections on an ASP.NET webpage with repeated items. Imagine something that gets rendered to HTML like this:

    <h2>First Header</h2>
    <div>Item 1</div>
    <div>Item 2</div>
    <h2>Second Header</h2>
    <div>Item 3</div>
    <div>Item 4</div>
    <div>Item 5</div>

    To avoid creating two repeaters, they created a single repeater, and then used the pre-render events to add headers to each section. Here's a short version of it (much simplified):

    <script runat="server">
    Private Sub myRepeater_PreRender(sender As Object, e As System.EventArgs) Handles myRepeater.PreRender
    For Each item In myRepeater.Items
    If SomeCondition1() Then
    Dim headerContainer As PlaceHolder = item.FindControl("myHeader")
    AddHeader(headerContainer, "First Header")
    End If
    If SomeCondition2() Then
    Dim headerContainer As PlaceHolder = item.FindControl("myHeader")
    AddHeader(headerContainer, "Second Header")
    End If
    Next
    End Sub
    </script>

    <asp:Repeater runat="server" ID="myRepeater">
    <ItemTemplate>
    <asp:PlaceHolder ID="myHeader" runat="server" />
    <%-- ...Rest of item template... -->
    </ItemTemplate>
    </asp:Repeater>

    Not a bad approach, I think, though I'd probably have just created a nested repeater:

    <asp:Repeater runat="server" ID="outerRepeater">
    <ItemTemplate>
    <h2><%# HttpUtility.HtmlEncode(DirectCast(Container.DataItem, ItemCollection).HeaderText) %></h2>
    <asp:Repeater runat="server" DataSource="<%# DirectCast(Container.DataItem, ItemCollection).Items %>">
    <ItemTemplate>
    <%-- ...Rest of item template... -->
    </ItemTemplate>
    </asp:Repeater>
    </ItemTemplate>
    </asp:Repeater>

    Of course, I'd have to split out the collection being bound to into two collections and create some wrapper classes and such, but nothing too difficult. I like this approach more, because you can avoid doing unnecessary processing during the render/binding stages (which just seems like a hack to me). Another reason I prefer my approach is because you could then sort each collection differently. Wh

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups