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
  <tr align="left">
        <%# DataBinder.Eval(Container, "dataItem.ID") %>
        <%# GetStatus(DataBinder.Eval(Container, "dataItem.ID")) %>

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:
Try this,
 <%# GetStatus(DataBinder.Eval(Container, "").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

Avatar of Rajar Ahmed
Rajar Ahmed
Flag of India image

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


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