The Web Jedi
Home of Halifax's Web Jedi
The Easy Way To Alternate Background Colours on a Repeater

The ASP.NET Repeater is one of my favourite controls because it provides total flexibility in the layout of your data. I'll often use it to generate a list of data similar to a DataGrid or DataList. However, if I want to alternate the background colour of each row of data, the usual prescribed method is to use the AlternatingItemTemplate. But this is a such a waste of code and can be a pain to maintain if your ItemTemplate has any kind of complexity.

There is a much easier method, though. A shortcut if you will. If you're using your Repeater to generate table rows, you can use the following code in your <tr> tag to change the CSS class for each alternating row:

<tr class="<%# IIf(Container.ItemIndex Mod 2 = 0, "rowOdd", "rowEven") %>">

Or, in C#:

<tr class="<%# Container.ItemIndex % 2 == 0 ? "rowOdd" : "rowEven" %>">

Easy peasy. This isn't really all that ingenious but every time I want to use this technique I forget the syntax (it's the "Container.ItemIndex" I can never remember) so I decided to write a blog entry so I'll have it for reference.  :-)


Posted 06-18-2007 10:21 PM by Andre Perusse
Filed under:

Comments

DotNetKicks.com wrote The Easy Way To Alternate Background Colours on a Repeater
on 06-19-2007 6:27 AM
You've been kicked (a good thing) - Trackback from DotNetKicks.com
Mike wrote re: The Easy Way To Alternate Background Colours on a Repeater
on 08-23-2007 10:14 PM
Thank you, I can't believe how long I've been searching for the Container.ItemIndex property!  Finally I can do alternating row styles "properly".
Ryan J. wrote re: The Easy Way To Alternate Background Colours on a Repeater
on 08-28-2007 6:57 AM
This was exactly what I was looking for.  Thank you for posting this!
Ashish wrote re: The Easy Way To Alternate Background Colours on a Repeater
on 09-16-2008 1:06 AM

Great great !!!!!!!!! just what i needed.

Joel H wrote re: The Easy Way To Alternate Background Colours on a Repeater
on 12-19-2008 2:49 AM

Thank you.  This is far superior to implementing the alternating item template!

Easter wrote re: The Easy Way To Alternate Background Colours on a Repeater
on 02-22-2009 10:47 PM

I am gonna use it

Stig wrote re: The Easy Way To Alternate Background Colours on a Repeater
on 02-26-2009 5:37 PM

Great!

imo bad of microsoft to not implement something gridview-like with the repeater..

thanks!

The Easy Way To Alternate Background Colours on a Repeater « Martin’s Blog wrote The Easy Way To Alternate Background Colours on a Repeater &laquo; Martin&#8217;s Blog
on 04-21-2009 2:58 AM

Pingback from  The Easy Way To Alternate Background Colours on a Repeater « Martin’s Blog

Rajasekhar wrote re: The Easy Way To Alternate Background Colours on a Repeater
on 06-18-2009 5:35 AM

It really works for me. Thanks for the solution.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Copyright - all rights reserved
Powered by Community Server (Non-Commercial Edition), by Telligent Systems