Repeater Control – Tips

28 08 2008

My recent project had a requirement to change the class for every fifth element in the repeater. Though I wasnt able to find it widely on the net, the coding turned to be easier than  I had ever imagined. Here’s the snippet that i used in the ItemDataBound event of the Repeater control to set the class for every fifth element:

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

Advertisements