Hi and welcome to my blog. Im Tasos, a software engineer working in the UK. This is where i share some of my findings related with SQL, c#, asp.net and javascript with you. I hope you find something helpful and Im looking forward to your feedback!

Recent Comments

Popular Posts

Recent Posts

Archives

Post Categories

Blog Stats

  • Posts: 15
  • Comments: 95
  • Trackbacks: 9
  • Articles: 1

Making your ASP:Repeater a little bit DRYer

Thursday, November 08, 2007 10:14 PM, Filed Under ASP.Net

image

Dont Repeat Yourself, we see it all over, but yet the suggested way of doing things with a Repeater control makes many developers repeat code. Let me explain what I mean, say we want to use a repeater to render a table containing rows of alternating colors. Sure there is a GridView for this, but there are some cases when we would like to use the more lightweight repeater (especially if we just want a readonly representation and do not want editing capabilities)


As you already know, alternating rows in the repeater can be implemented by entering corresponding markup in the <ItemTemplate> tag for one style and more markup in the <AlternatatingItemTemplate> for the alternating template code. So we basically manage to end up with duplicate code just to set our style.

I mean, just look at the following snippet. The main markup is placed twice just to set the alternating row style.

<table>
    <asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
            <tr class="RowStyle0">
                <td>
                    <%# DataBinder.Eval(Container.DataItem, "FirstName") %>
                </td>
                <td>
                    <%# DataBinder.Eval(Container.DataItem, "LastName") %>
                </td>
                <td>
                    <%# DataBinder.Eval(Container.DataItem, "Age") %>
                </td>
            </tr>
        </ItemTemplate>
        <AlternatingItemTemplate>
            <tr class="RowStyle1">
                <td>
                    <%# DataBinder.Eval(Container.DataItem, "FirstName") %>
                </td>
                <td>
                    <%# DataBinder.Eval(Container.DataItem, "LastName") %>
                </td>
                <td>
                    <%# DataBinder.Eval(Container.DataItem, "Age") %>
                </td>
            </tr>
        </AlternatingItemTemplate>
    </asp:Repeater>
</table>

I just dont like seeing <AlternatingItemTemplate> in my code for something as simple as setting the style of a row.
The workaround is just to use the ItemTemplate and determine the css class of the each TR by evaluating the Container.ItemIndex modulo 2. So, here it goes:

<table>
    <asp:Repeater ID="Repeater2" runat="server">
        <ItemTemplate>
            <tr class="RowStyle<%# Container.ItemIndex % 2 %>">
                <td>
                    <%# DataBinder.Eval(Container.DataItem, "FirstName") %>
                </td>
                <td>
                    <%# DataBinder.Eval(Container.DataItem, "LastName") %>
                </td>
                <td>
                    <%# DataBinder.Eval(Container.DataItem, "Age") %>
                </td>
            </tr>
        </ItemTemplate>
    </asp:Repeater>
</table>

Just make sure you have a .RowStyle0{ } and .RowStyle1{ } in your css with your colors of choice

Now this is a simple showcase example, imagine having a more complex repeater (containing more columns). IMHO, avoiding the AlternatingItemTemplate leeds to DRYer, more maintainable code. Overall this is a very simple example but im pretty sure that we've all come across AlternatingItemTemplate for such a trivial task.

Share this post!
digg it
Kick it on DotNetKicks.com

Comments

# re: Making your ASP:Repeater a little bit DRYer, Posted by Jon on 4/7/2009 4:45 PM

Ah! Nice tip there, Tasos!
In fact I wasn't trying to set the alternating row style - I wanted to render the row number of the repeater to build the hidden fields needed to submit a shopping basket to PayPal. Nonetheless, I hadn't thought of using properties of the Container within the repeater markup, so thanks for saving me some head scratching.
I agree - repeating markup is asking for trouble. Many's the time I've corrected an error in one fork but not the other. I usually do stuff in _ItemDataBound now but I like your technique because it keeps the purely visual stuff in the markup.
Thanks for taking the time to write that up.
Cheers
Jon.

# re: Making your ASP:Repeater a little bit DRYer, Posted by free ebook on 5/11/2009 2:11 AM

Thanks for the great reference post.

# re: Making your ASP:Repeater a little bit DRYer, Posted by Oes Tsetnoc on 10/2/2009 5:44 PM

previously, i was really confuse about it. yet you give me enlightment. thanks.

Comments

Title: *
Name: *
Email: (never displayed)
Website:
Comment: *  
Please add 4 and 2 and type the answer here: