back to Jitbit Blog home About this blog

ASP.NET Repeater Alternative Item Markup Optimization

by Alex Yumashev · Sep 30 2010
Another post for ASP.NET/SQL developers reading this blog. If you think these posts do not belong here, please leave a comment, and I'll consider moving my development articles to a separate blog.

Sometimes when you use the ASP.NET repeater control, you utilize the "alternating item" template. And sometimes the alternative item's design is only slightly different from the regular item. So a huge part of the HTML-code is just being duplicated like this:
<ItemTemplate>
<tr class='regularItem'>
<!-- lots of code -->
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr class='alternativeItem'>
<!-- lots of THE SAME code -->
</tr>
</AlternatingItemTemplate>

Here's a great simple tip that shortens the ASP.NET repeater control markup by 50%:

<ItemTemplate>
<%# Container.ItemType == ListItemType.AlternatingItem ? "<tr class='alternativeItem'>" : "<tr class='regularItem'>" %>
<!-- lots of code -->
</tr>
</ItemTemplate>

As you can see, instead of creating a separate AlternatingItemTemplate just to change the class name, you can determine, what item is it - right inside the repeater control markup.