How can I apply alternating styles to items in a repeater?

HelenaG Grulichova asked on August 7, 2012 05:13

How can I apply alternating styles to items in a repeater?

Correct Answer

HelenaG Grulichova answered on August 7, 2012 05:13

You can do this by making use of the DataItemIndex property.
 
The following example will create an opening div tag that uses a different class for odd and even items:
 

<%-- Add one to zero-indexed DataItemIndex --%>
<%# ((1+DataItemIndex)%2)==0?"<div class='even'>":"<div class='odd'" %>


 
See the following Knowledgebase article for more details on special properties available in transformations:
Using special item data in transformation
 

-ag-

 

0 votesVote for this answer Unmark Correct answer

Recent Answers


web dev answered on December 7, 2017 13:44

That <%# ((DataItemIndex%2)==0?"

":"<div class='odd'" %> is correctly

0 votesVote for this answer Mark as a Correct answer

Youn IRAGUI answered on December 7, 2017 14:03

I see that the right solution is: DataItemIndex%2

0 votesVote for this answer Mark as a Correct answer

Youness IRAGUI answered on December 8, 2017 13:11 (last edited on December 8, 2017 13:13)

Here is the effective solution: DataItemIndex Mod 2

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.