ASP.NET - 更改标签颜色并与字符串连接

ASP.NET - Change label color and conc with string

我创建了一个标签并将他的文本颜色设置为红色。我使用 ForeColor 属性 但他不工作 :(

这是我的UI:

这是我的标签代码:

        Label reqF = new Label();
        reqF.Text = "*";
        reqF.ForeColor = System.Drawing.Color.Red;
        reqF.CssClass = "formvalidation";
        reqF.Font.Size = 15; // 15px

这是我的字符串代码:

        TableHeaderCell header1 = new TableHeaderCell();
        header1.Text = "Destination" + reqF.Text;  <----------- My label
        tRow0.Cells.Add(header1);

在这一行

header1.Text = "Destination" + reqF.Text; 

您只将文本 (*) 添加到目的地,而不是带有样式的完整 div。

最简单(也更快)的方法是直接在线添加样式为:

header1.Text = "Destination <span style=\"color=red\">*</span>" ;

您只使用了控件的文本,而不是整个带有样式的控件。您的代码实际上应该看起来像这样:

TableHeaderCell header1 = new TableHeaderCell();
LiteralControl literal = new LiteralControl();
literal.Text = "Destination";
header1.Controls.Add(literal);
header1.Controls.Add(reqF);
tRow0.Cells.Add(header1);

注意这里Literal控件是用来插入Description字符串的,然后插入的是整个带有*的Label。