将文本框/标签中文本的颜色设置为红色,并在 asp.net C# 中将其设为粗体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9990735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set color of text in a Textbox/Label to Red and make it bold in asp.net C#
提问by Ishan
I want a text color to be red in color on certain condition.
我希望文本颜色在特定条件下为红色。
Here is how i want to get it done.
这是我想要完成的方法。
string minusvalue = TextBox1.Text.ToString();
if (Convert.ToDouble(minusvalue) < 0)
{
// set color of text in TextBox1 to red color and bold.
}
Is there any function that can set the property of text in TextBox? Please help!
有没有可以设置TextBox中文本属性的函数?请帮忙!
采纳答案by Curt
TextBox1.ForeColor = Color.Red;
TextBox1.Font.Bold = True;
Or this can be done using a CssClass (recommended):
或者这可以使用 CssClass (推荐)来完成:
.highlight
{
color:red;
font-weight:bold;
}
TextBox1.CssClass = "highlight";
Or the styles can be added inline:
或者可以内联添加样式:
TextBox1.Attributes["style"] = "color:red; font-weight:bold;";
回答by Kristof
Try using the property ForeColor. Like this :
尝试使用属性 ForeColor。像这样 :
TextBox1.ForeColor = Color.Red;
回答by SkonJeet
string minusvalue = TextBox1.Text.ToString();
if (Convert.ToDouble(minusvalue) < 0)
{
// set color of text in TextBox1 to red color and bold.
TextBox1.ForeColor = Color.Red;
}
回答by Darren Wood
Another way of doing it. This approach can be useful for changing the text to 2 different colors, just by adding 2 spans.
另一种方法。这种方法可用于将文本更改为 2 种不同的颜色,只需添加 2 个跨度即可。
Label1.Text = @"<b><span style=""color:red;"">" + "Your String Here" + "</span></b>";

