1

I have a Repeater item in which their is a tr elements like this. I want to set css class according to item index as alternating item.

My Design Code is Like This

    <ItemTemplate>
            <tr id='<%# Eval("RegNum").ToString()%>' class='<%# Eval("RowClass").ToString()%>'>

Item Data Bound like this but not working: ( Its Wrong )

        if (e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DataBinder.Eval("RowClass", "bgBlack");
        }
        else
        {
            DataBinder.Eval("RowClass", "bgWhite");
        }

where bgWhite and bgBlack are css class.

Suggest Me how to achieve this.

Saurabh Sharma
  • 139
  • 1
  • 12

1 Answers1

3

you can see if the built-in ItemIndex property is divisible by two, and use that to set a css class:

class="<%# Container.ItemIndex % 2 == 0 ? "bgWhite" : "bgBlack" %>"

This has the benefit of being completely based in your UI code (ascx or aspx file), and doesn't rely on JavaScript.

Please see the below link

ASP.NET repeater alternate row highlighting without full blown <alternatingitemtemplate/>

Community
  • 1
  • 1
Deepak Joshi
  • 968
  • 6
  • 15