Html 从 ASP.NET 代码更改 CSS 样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/403031/
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
Changing CSS style from ASP.NET code
提问by David Bonnici
Possible Duplicate:
Change CSS Dynamically
可能的重复:
动态更改 CSS
I need to change the height of a div container(CSS Property Height) from ASP.NET code (VB).
我需要从 ASP.NET 代码 (VB) 更改 div 容器的高度(CSS 属性高度)。
How can I do that?
我怎样才能做到这一点?
回答by Tom Ritter
C#, because I don't want to typo the VB syntax.
C#,因为我不想打错 VB 语法。
Markup:
标记:
<div runat="server" id="divControl">...</div>
<div runat="server" id="divControl">...</div>
Class of the Page:
页面类别:
protected System.Web.UI.HtmlControls.HtmlGenericControl divControl;
protected System.Web.UI.HtmlControls.HtmlGenericControl divControl;
OnLoad/Other function:
OnLoad/其他功能:
divControl.Style.Add("height", number / anotherNumer);
divControl.Style.Add("height", number / anotherNumer);
回答by Lurker Indeed
VB Version:
VB版本:
Class:
班级:
Protected divControl As System.Web.UI.HtmlControls.HtmlGenericControl
OnLoad/Other function:
OnLoad/其他功能:
divControl.Style("height") = "200px"
I've never tried the Add method with the styles. What if the height already exists on the DIV?
我从未尝试过使用样式的 Add 方法。如果高度已经存在于 DIV 上怎么办?
回答by Peanut
As a NOT TO DO - Another way would be to use:
作为 NOT TO DO - 另一种方法是使用:
divControl.Attributes.Add("style", "height: number");
But don't use this as its messy and the answer by AviewAnew is the correct way.
但是不要因为它的混乱而使用它,AviewAnew 的答案是正确的方法。
回答by Eugene Katz
If your div is an ASP.NET control with runat="server" then AviewAnew's answer should do it. If it's just an HTML div, then you'd probably want to use JavaScript. Can you add the actual div tag to your question?
如果您的 div 是一个带有 runat="server" 的 ASP.NET 控件,那么 AviewAnew 的回答应该这样做。如果它只是一个 HTML div,那么您可能想要使用 JavaScript。你能在你的问题中添加实际的 div 标签吗?
回答by a7drew
I find that code gets messy fast when C# code is used to modify CSS values. Perhaps a better approach is for your code to dynamically set the class attribute on the div tag and then store any specific CSS settings in the style sheet.
我发现当使用 C# 代码修改 CSS 值时,代码会很快变得混乱。也许更好的方法是让您的代码在 div 标签上动态设置 class 属性,然后将任何特定的 CSS 设置存储在样式表中。
That might not work for your situation, but its a decent default position if you need to change the style on the fly in server side code.
这可能不适用于您的情况,但如果您需要在服务器端代码中即时更改样式,它是一个不错的默认位置。