In this blog we will know how to Change the fore color of an
item in a repeater based on the status.
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default"
%>
<!DOCTYPE html
PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form
id="form1"
runat="server">
<div>
<asp:Repeater ID="Repeater1"
runat="server"
>
<HeaderTemplate>
<table
border="1"
cellpadding="0"
cellspacing="0">
</HeaderTemplate>
<ItemTemplate>
<tr
style="color: <%# GetRowColor(DataBinder.Eval(Container.DataItem,
"status")) %>">
<td
width="70px"
align="center"
valign="middle">
<asp:Label ID="lblID" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem,
"ID") %>' />
</td>
<td
valign="middle"
width="70px">
<%#DataBinder.Eval(Container.DataItem,
"status") %>
</td>
<td
valign="middle"
width="70px">
<%#DataBinder.Eval(Container.DataItem,
"name") %>
</td>
<td
valign="middle"
width="70px">
<%#DataBinder.Eval(Container.DataItem,
"address")%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial
class _Default
: System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt =
CreateDataSource();
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
}
protected DataTable CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("ID",
typeof(string)));
dt.Columns.Add(new DataColumn("status",
typeof(string)));
dt.Columns.Add(new DataColumn("name",
typeof(string)));
dt.Columns.Add(new DataColumn("address",
typeof(string)));
dr = dt.NewRow();
dr["ID"] = "E001";
dr["status"] =
"1";
dr["name"] = "Raj";
dr["address"]
= "pune";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["ID"] = "E002";
dr["status"] =
"2";
dr["name"] = "ravi";
dr["address"]
= "mumbai";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["ID"] = "E003";
dr["status"] =
"3";
dr["name"] = "rahul";
dr["address"]
= "puri";
dt.Rows.Add(dr);
return dt;
}
public string
GetRowColor(object obj)
{
string color = "white";
if (!string.IsNullOrEmpty(obj.ToString()))
{
string status =
obj.ToString();
switch (status)
{
case "1":
color = "red";
break;
case "2":
color = "green";
break;
case "3":
color = "blue";
break;
default:
color = "white";
break;
}
}
return color;
}
}