ASP Repeater helper to output class names on every Nth item
06 Feb 2014
Reading time: 1 minute
I came across a need to output a “clear” class to every 5th item in an asp:repeater. Here’s an over-simplified example of the code that was written.
I say we could to better. First - let’s work out what we want to say. Every 5th item output a new class.
To determine if we are the fith item, we need to know the currentIndex and add 1 as it’s 0 based.
So let’s write pseudo code of what we actually want:
Okay - that feels better, but I think we could improve it by having ‘5’ as a parameter, I know that we might decide to change the width of this repeater later, and thus need to change which item clears.
Nice - that’s a descriptive way of outputting a the “clear” class on every 5th item, and allows us to change the params depending on our needs. But how do we write the code?
So this is the skeleton that allows us to extend the “Container” object in a repeater, adding “Every” as a function to any IDataItemContainer in your project. Now we need to return an object that exposes an Output function…
ANd that’s it - outputting a class on every 5th item in a repeater.