Search code examples
c#asp.netcolorsdatabinder

How to change color of text in rows with DataBinder


I want to change color of text in specific column in each of rows. I have several columns, one of them is named DeliverState which can obtain one of two states - "Delivered" or "NotDelivered"

<td><%# DataBinder.Eval(Container.DataItem, "dbs").ToString()=="1" ? "" : DataBinder.Eval(Container.DataItem, "deliver_state")%></td>

I want to change color of the text (which can be Delivered or NotDelivered) in each row in column DeliverState to green if "Delivered" and red if "NotDelivered".

What is the best way to do it in .aspx file?


Solution

  • Maybe add a class to the items based on your if condition? that way you can add a styling for e.g

    .NotDelivered
    { 
      color:Red
    }
    
    .Delivered
    {
       color:Green
    }
    
    
    <td class="<%#Evaluated your choice : "delivered" : "notDelivered"><...the rest of your statement ..></td>
    

    I would maybe suggest that you put the eval into a a variable that you can call on evaluation of the tenary operator.