Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 仅为标签asp.net的一部分设置背景色_C#_Asp.net - Fatal编程技术网

C# 仅为标签asp.net的一部分设置背景色

C# 仅为标签asp.net的一部分设置背景色,c#,asp.net,C#,Asp.net,我正在向页面动态添加标签,有一些标签必须根据需要进行标记。下面是代码 Panel1.Controls.Add(new LiteralControl("<td>")); if (required) { Mynewlabel.Text = lblname + " *"; Mynewlabel.BackColor = System.Drawing.Color.Red; } Co

我正在向页面动态添加标签,有一些标签必须根据需要进行标记。下面是代码

 Panel1.Controls.Add(new LiteralControl("<td>"));
        if (required)
        {
            Mynewlabel.Text = lblname + " *";
            Mynewlabel.BackColor = System.Drawing.Color.Red;
        }
        ControlsPanel.Controls.Add(Mynewlabel);
        ControlsPanel.Controls.Add(new LiteralControl("</td>"));
Panel1.Controls.Add(新的LiteralControl(“”);
如果(需要)
{
Mynewlabel.Text=lblname+“*”;
Mynewlabel.BackColor=System.Drawing.Color.Red;
}
ControlsPanel.Controls.Add(Mynewlabel);
ControlsPanel.Controls.Add(新的LiteralControl(“”);
设置Mynewlabel.BackColor会使整个标签的背面颜色变为红色,而我只希望*符号变为红色…我不确定它是否可行

请提供您的输入。

将星号(*)放在自己的span标记中,并通过CSS类对其应用颜色


更优雅的方法是从标签继承您自己的自定义标签,并将CSS规则作为属性应用于该标签。通过这种方式,您可以在一个页面上收集这些内容,而不会留下混乱的代码:)

我相信您可以这样做:

Mynewlabel.Text = lblname + @" <span style=""background-color:red"">*</span>";
Mynewlabel.Text=lblname+@“*”;

这似乎有点粗糙,但如果您想要在一个标签中描述的样式,这可能是您唯一的选择。

这是可行的,但很难看:

Mynewlabel.Text = lblname + @"<span style=""color:red;""> *</span>";
Mynewlabel.Text=lblname+@“*”;
CSS:

.required
{
   color:red;
}
Panel1.Controls.Add(new LiteralControl("<td>"));
if (required)
{
    Mynewlabel.Text = lblname;
    Label lblRequired = new Label();
    lblRequired.Text = "*";
    lblRequired.CssClass= "required";
}
ControlsPanel.Controls.Add(Mynewlabel);
ControlsPanel.Controls.Add(lblRequired);
ControlsPanel.Controls.Add(new LiteralControl("</td>"));
代码:

.required
{
   color:red;
}
Panel1.Controls.Add(new LiteralControl("<td>"));
if (required)
{
    Mynewlabel.Text = lblname;
    Label lblRequired = new Label();
    lblRequired.Text = "*";
    lblRequired.CssClass= "required";
}
ControlsPanel.Controls.Add(Mynewlabel);
ControlsPanel.Controls.Add(lblRequired);
ControlsPanel.Controls.Add(new LiteralControl("</td>"));
Panel1.Controls.Add(新的LiteralControl(“”);
如果(需要)
{
Mynewlabel.Text=lblname;
Label lblRequired=新标签();
lblRequired.Text=“*”;
lblRequired.CssClass=“必需”;
}
ControlsPanel.Controls.Add(Mynewlabel);
ControlsPanel.Controls.Add(lblRequired);
ControlsPanel.Controls.Add(新的LiteralControl(“”);

谢谢。我刚做了我的新标签。Text=lblname+@“*”;