Link to home
Create AccountLog in
Avatar of CipherIS
CipherISFlag for United States of America

asked on

ASP.NET Set Color based on Status

I am using a repeater to display status of an item.  I created a method "GetStatus" to return "incomplete" or "complete".  I would like this to display as red if it is "incomplete" and "green" if it is complete as the data loads in the repeater.  Any ideas?

Small code sample
<ItemTemplate>
  <tr align="left">
    <td>
        <%# DataBinder.Eval(Container, "dataItem.ID") %>
    </td>
    <td>
        <%# GetStatus(DataBinder.Eval(Container, "dataItem.ID")) %>
    </td>
  </tr>
</ItemTemplate>

Open in new window

Avatar of Daniel Van Der Werken
Daniel Van Der Werken
Flag of United States of America image

I would do this in the RowDataBound event.

First, you'd vind the repeater to a data source that contained all of the information you needed.

Then, based off the info, you could set the colore (text color?) of the html element based off the status. The element would need to have a runat="server" set. You'd need to do a FindControl(...) within the event to grab that element.

Do you need an example for this?  Here are some sites:
http://www.asp.net/web-forms/tutorials/data-access/displaying-data-with-the-datalist-and-repeater/formatting-the-datalist-and-repeater-based-upon-data-cs

http://www.aspdotnet-suresh.com/2012/06/get-find-controls-inside-of-aspnet.html

http://www.aspdotnet-suresh.com/2012/06/get-find-controls-inside-of-aspnet.html
Try this,
Aspx:
 <%# GetStatus(DataBinder.Eval(Container, "dataItem.id").ToString()) %>

Open in new window

Code behind
public static string GetStatus(string status)
    {
        if (status.ToLower() == "incomplete")
        {
            return "cssRed";//Have class in css .cssRed{color:red;}
        }
        else {
            return "cssGreen";//Have class in css .cssGreen{color:green;}
        }

    }

Open in new window

Meeran03
ASKER CERTIFIED SOLUTION
Avatar of Rajar Ahmed
Rajar Ahmed
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of CipherIS

ASKER

I modified your solution a bit and got it to work.

Thanks