在一项目中,前台要根据状态来分别显示不同的行背景色,如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);
            }
        }
posted on 2013-05-10 20:23  晓晨  阅读(4429)  评论(3编辑  收藏  举报