protected void gvzh_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[0].Visible = false;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[3].Text == "已结束")
            {
                e.Row.Attributes.Add("style", "color:#008000");
            }
            else if (e.Row.Cells[3].Text == "待实施")
            {
                e.Row.Attributes.Add("style", "color:#0000ff");
            }
            else if (e.Row.Cells[3].Text == "待审核")
            {
                e.Row.Attributes.Add("style", "color:#ff0000");
            }
            else if (e.Row.Cells[3].Text == "被取消")
            {
                e.Row.Attributes.Add("style", "color:#bbbbbb");
            }
            else if (e.Row.Cells[3].Text == "待回访")
            {
                e.Row.Attributes.Add("style", "color:#8A6800");
            }
            else if (e.Row.Cells[3].Text == "退货")
            {
                e.Row.Attributes.Add("style", "color:#848284");
            }
        }
    }
这里gridview  我通过判断某一列的值 来让其显示指定颜色,现在的问题是,换成Repeater,如何通过判断某列的值来让其显示指定颜色,在线等

解决方案 »

  1.   

     <asp:Repeater ID="rptList" runat="server" >    
        <ItemTemplate>   
        <div style='<%#  GetColor( Eval("绑定值名称")) %>'></div>
        </ItemTemplate>
        </asp:Repeater>
    ------------------------------------
     public static string GetColor(object obj)
        {
            if (obj == DBNull.Value)
            {
                return string.Empty;
            }
            return GetColor(obj.ToString());
        }    public static string GetColor(string val)
        {
            if (val == "已结束")
            {
                return "color:#008000";
            }
            else if (val == "待实施")
            {
                return "color:#0000ff";
            }
            else
            {
                return string.Empty;
            }
        }
      

  2.   

    Repeater的ItemDataBound事件中处理一下<asp:Repeater ID="rpt" runat="server" OnItemDataBound="rptStatus_ItemDataBound">
        <ItemTemplate>
                 <asp:Label ID="lbStatus" runat="server" Text='<%#Eval("数据库字段名称")%>'></asp:Label>
         </ItemTemplate>
    </asp:Repeater>
    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label lb = (Label )e.Item.FindControl("lbStatus");    
            swith(lb.Text)
            {
                case "已结束":
                         lb.ForeColor = "Red";
                         break;
                ......
            }   
        }
    }
      

  3.   

    同理 激发OnItemDataBound事件
    我的思路和4楼差不多  
      

  4.   

     <span style='<%#  show( Eval("id").ToString())%>' > <%#Eval("id")%> </span></td>
    .cs:
    public string show(string str)
        {
            if (str == "1")
            {
                return "color:red";
            }        else
            {
                return "color:green";
            }
        }
      

  5.   

    2#的方法挺好,不产生多余的ViewState
      我做也会这么写