在html属性中输入不同的颜色文本

时间:2015-01-19 13:27:00

标签: c# html css asp.net

我正在使用asp.net,我想根据参数填充具有不同颜色的表格单元格的文本属性。例如:

TableCell dataCell= new TableCell();
            foreach (var o in results)
            {
                TimeSpan timeDiff = (DateTime.Now - o.time);
                if (timeDiff.TotalSeconds < 60.0)
                {
                    //Here with green color
                    dataCell.Text += o.name;
                    //I tried dataCell.Text += string.Format("<p //style=\"color":green\" src='{0}'>", o.name); but doesn't work. 
                }
                else
                {
                    //Here with red color
                    dataCell.Text += o.name;
                }                   
            }
            TRow.Cells.Add(dataCell);  

我想将文字放在一行中,因此<h3><div><p>对我不起作用。

1 个答案:

答案 0 :(得分:0)

如果要为竞争单元格设置它,请使用ForeColor property

if (timeDiff.TotalSeconds < 60.0)
{
    //Here with green color
    dataCell.ForeColor = System.Drawing.Color.Green;
    dataCell.Text += o.name;
    //I tried dataCell.Text += string.Format("<p //style=\"color":green\" src='{0}'>", o.name); but doesn't work. 
}
else
{
    //Here with red color
    dataCell.ForeColor = System.Drawing.Color.red;
    dataCell.Text += o.name;
}

否则您可以向单元格添加其他控件,例如Labels。但请注意,您需要在每次回发时重新创建每个动态创建的控件。