Repeater enhancements

Repeater control is one of the most flexible controls available in ASP.NET. Still, Open Waves adds a couple of useful features. To use the features use OpenWaves.Web.Controls.Repeater instead of a regular repeater.

Repeater template

To surround items rendered by a repeater with additional markup header and footer templates can be used. OpenWaves.Web.Repeater offers another way:

<ow:Repeater runat="server" DataSource="...">
    <Template>
        <ul><ow:ItemsPlaceHolder runat="server"/></ul>
    </Template>
    <ItemTemplate>
        <li><%#Container.DataItem %></li>
    </ItemTemplate>
</ow:Repeater>

ItemsPlaceHolder control, when used inside the Template property will be used as a placeholder for items rendered by the repeater. Header and Footer templates still can be used and will be rendered inside ItemsPlaceHolder.

FirstItemTemplate

It is sometimes required to present the first item in the list differently then the rest of them. To achieve that you can use FirstItemTemplate property of the mw:Repeater.

<ow:Repeater runat="server" DataSource="<%#this.DataSource%>">
    <FirstItemTemplate>
        First Item: <%#Container.DataItem %>
    </FirstItemTemplate>
    <ItemTemplate>
        <%#Container.DataItem %>
    </ItemTemplate>
</ow:Repeater>

Alternate

More then often we are required to highlight alternating repeater rows. Most of the times we just use AlternatingItemTemplate property but this requires us to copy the whole item template.

If the only thing you want to change is for example class name of the item a helper method Cycle can be used. The method cycles trough the list of supplied strings with every iteration of the repeater. Please note that Cycle method has been implemented as an extension method on IDataItemContainer interface so it can be used not only within repeater templates but also within any other template of repeating controls.

<ow:Repeater runat="server" DataSource="<%#this.DataSource%>">
    <ItemTemplate>
        <li class='<%#Container.Cycle("odd", "even")%>'>
            <%#Container.DataItem %>
        </li>
    </ItemTemplate>
</ow:Repeater>

NoResultsTemplate

If it is required to show a different content when the list we’re binding a repeater to is empty the following snippet can be used.

<ow:Repeater runat="server" DataSource="<%#this.DataSource%>">
    <NoResultsTemplate>
        No results
    </NoResultsTemplate>
    <ItemTemplate>
        ...
    </ItemTemplate>
</ow:Repeater>

Items grouping

Sometimes the items rendered by the repeater need to be put in groups. An example can be a multicolumn listing (sometimes this can be solved solely by smart css).
To group items, specify the group size as an attribute of the repeater (default is 1) and provide a group template. Inside the group template OpenWaves.Web.ItemsPlaceHolder can be used to indicate where the items belonging to the group should be rendered.
Note: If FirstItemTemplate is used with GroupTemplate, it will be used for a first item in every group.

<ow:Repeater runat="server" DataSource="<%#this.DataSource%>" GroupSize="2">
    <GroupTemplate>
        <div><ow:ItemsPlaceHolder runat="server" /></div>
    </GroupTemplate>
    <ItemTemplate>
        <span><%#Container.DataItem %></span>
    </ItemTemplate>
</ow:Repeater>

Last edited Jul 8, 2011 at 7:31 AM by mgrzyb, version 2

Comments

No comments yet.