C# 使用后面的代码设置 div 的 css 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11057839/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:19:42  来源:igfitidea点击:

Set the css class of a div using code behind

c#asp.netvb.net

提问by wotney

I understand that we can set the various style attributes from code behind using :

我知道我们可以使用后面的代码设置各种样式属性:

divControl.Style("height") = "200px"

But how to set it's class instead of declaring each of the styles from code?

但是如何设置它的类而不是从代码中声明每个样式?

采纳答案by Claudio Redi

C#

C#

divControl.Attributes["class"] = "myClass";

VB

VB

divControl.Attributes("class") = "myClass"

You'll need to have a rule like this one on your css file

你需要在你的 css 文件上有一个这样的规则

.myClass
{
   height:200px; 
   /*...more styles*/
}

回答by Yuriy Galanter

This way you can define full style inline, without separate class declaration:

通过这种方式,您可以定义完整的内联样式,而无需单独的类声明:

divControl.Attributes("style") = "height:200px; color:Red"