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

Enter different color text in an html attribute

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

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> 对我不起作用。

如果要为竞争单元格设置,请使用 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。但请注意,您需要在每次回发时重新创建每个动态创建的控件。