Set Class Using Eval in Item Data Bound

2.1k Views Asked by At

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.

1

There are 1 best solutions below

0
Deepak Joshi On BEST ANSWER

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/>