vb.net 如何使用 ASP.NET 从后面的代码中添加额外的 css 类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25161335/
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
How to add an additional css class from code behind using ASP.NET?
提问by DreamTeK
How to add an additional css class from code behind using ASP.NET?
如何使用 ASP.NET 从后面的代码中添加额外的 css 类?
CURRENT TEXTBOX
当前文本框
<asp:TextBox ID="txt" CssClass="MyClass" runat="Server" />
DESIRED OUTPUT
期望的输出
<asp:TextBox ID="txt" CssClass="MyClass Error" runat="Server" />
Testing
测试
txt.CssClass = "Error"
This replaces current css class.
这将替换当前的 css 类。
txt.CssClass = "MyClass Error"
This works but but is greatly inefficient having to specify class.
这有效,但必须指定类的效率非常低。
txt.Attributes.Add("class", "Error")
This only works if no initial class is set.
这仅在未设置初始类时才有效。
txt.Attributes("class") += " Error"
This does not work for me.
这对我不起作用。
回答by DreamTeK
Add additional CssClass like this:
添加额外的 CssClass 像这样:
txt.CssClass = txt.CssClass + " Error"
The above can also be abbraviated as:
以上也可以简写为:
txt.CssClass += " Error"
Credit to @Mikey and @MelanciaUK for their input.
感谢@Mikey 和@MelanciaUK 的贡献。
回答by Rob W.
I know that you were looking for a quick one-liner. However, this previous answer may prove useful going forward:
我知道您正在寻找一种快速的单线。但是,这个先前的答案可能会在未来证明有用:

