C# 如何在 ASP.NET 中的 div 代码隐藏文件中修改 CSS 样式?

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

How do you modify a CSS style in the code behind file for divs in ASP.NET?

c#asp.netcss

提问by EverTheLearner

I'm trying to modify a CSS style attribute for a div based on the information I get from a database table in the code behind of my aspx page. The following is essentially what I am trying to do, but I get errors.

我正在尝试根据我从 aspx 页面后面的代码中的数据库表中获取的信息修改 div 的 CSS 样式属性。以下基本上是我想要做的,但我得到了错误。

Aspx:

ASP:

<div id="testSpace" runat="server">
    Test
</div>

Code Behind:

背后的代码:

testSpace.Style = "display:none;"    
testSpace.Style("display") = "none";

What am I doing wrong?

我究竟做错了什么?

采纳答案by Andy White

testSpace.Style.Add("display", "none");

回答by nickytonline

It's an HtmlGenericControl so not sure what the recommended way to do this is, so you could also do:

这是一个 HtmlGenericControl 所以不确定推荐的方法是什么,所以你也可以这样做:

testSpace.Attributes.Add("style", "text-align: center;");

or

或者

testSpace.Attributes.Add("class", "centerIt");

or

或者

testSpace.Attributes["style"] = "text-align: center;";

or

或者

testSpace.Attributes["class"] = "centerIt";

回答by Nikolaj Zander

Another way to do it:

另一种方法:

testSpace.Style.Add("display", "none");

or

或者

testSpace.Style["background-image"] = "url(images/foo.png)";

in vb.net you can do it this way:

在 vb.net 你可以这样做:

testSpace.Style.Item("display") = "none"