Changing the display of every nth element in a repeater

I recently had to change the class on every third data element in a repeater. And it turned out to be easier than I thought it might be, but I did not find it widely documented. The following snippet is called on the ItemDataBound event on the repeater and sets the class on a div for every third item in the repeater:


private void rptCollections_ItemDataBound( object sender, RepeaterItemEventArgs e ) {
if ( ( e.Item.ItemType == ListItemType.Item e.Item.ItemType == ListItemType.AlternatingItem )
&& ( 1 + e.Item.ItemIndex ) % 3 == 0 ) {
HtmlGenericControl divItem = e.Item.FindControl( "divItem" ) as HtmlGenericControl;
divItem.Attributes.Remove( "class" );
divItem.Attributes.Add( "class", "lastItemCollectionItem" );
}
}

0 comments: (+add yours?)