在一项目中,前台要根据状态来分别显示不同的行背景色,如1红色,2绿色,3黑色
只需要添加Repeater1_ItemDataBound即可,代码如下:
protected void Repeater1_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
try
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var iErrorCou = int.Parse(((DataRowView) (e.Item.DataItem)).Row[5].ToString());
var iAlterCou = int.Parse(((DataRowView) (e.Item.DataItem)).Row[6].ToString());
var sStatus = ((DataRowView) (e.Item.DataItem)).Row[11].ToString();
switch (sStatus)
{
case "1":
if (iErrorCou >= iAlterCou)
{
((HtmlTableRow) e.Item.FindControl("row")).BgColor = "red";
//((HtmlTableRow)e.Item.FindControl("row")).Style.Add("background-color","red !important");
}
else
{
((HtmlTableRow)e.Item.FindControl("row")).BgColor = "green";
}
break;
case "2":
((HtmlTableRow)e.Item.FindControl("row")).BgColor = "black";
break;
default:
((HtmlTableRow)e.Item.FindControl("row")).BgColor = "#01439A";
break;
}
}
}
catch (Exception ex)
{
Common.Log.Error("更改背景颜色出错:" + ex);
}
}
{
try
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var iErrorCou = int.Parse(((DataRowView) (e.Item.DataItem)).Row[5].ToString());
var iAlterCou = int.Parse(((DataRowView) (e.Item.DataItem)).Row[6].ToString());
var sStatus = ((DataRowView) (e.Item.DataItem)).Row[11].ToString();
switch (sStatus)
{
case "1":
if (iErrorCou >= iAlterCou)
{
((HtmlTableRow) e.Item.FindControl("row")).BgColor = "red";
//((HtmlTableRow)e.Item.FindControl("row")).Style.Add("background-color","red !important");
}
else
{
((HtmlTableRow)e.Item.FindControl("row")).BgColor = "green";
}
break;
case "2":
((HtmlTableRow)e.Item.FindControl("row")).BgColor = "black";
break;
default:
((HtmlTableRow)e.Item.FindControl("row")).BgColor = "#01439A";
break;
}
}
}
catch (Exception ex)
{
Common.Log.Error("更改背景颜色出错:" + ex);
}
}