简体   繁体   English

如何从后面的代码中将CSS样式添加到ASP.NET UserControl?

[英]How do I add CSS styles to an ASP.NET UserControl from the code-behind?

this.Style.Add ( /*...*/ )

... doesn't appear to work. ...似乎无效。

What do I need to do to add a style to it? 我需要做什么才能为其添加样式?

Think about the nature of UserControl - it's intended to contain other ASP.NET controls and that's why you cannot set CSS for whole control.. instead you just need to specify styles for each child control individually. 考虑一下UserControl的性质-它打算包含其他ASP.NET控件,这就是为什么您不能为整个控件设置CSS的原因。相反,您只需要分别为每个子控件指定样式即可。 You can also take a look at the ApplyStyleSheetSkin method. 您还可以查看ApplyStyleSheetSkin方法。

您可以将用户控件包装到另一个html容器(例如div)中,并在容器上设置样式。

If your user control is inheriting from webcontrol simply use the system.web.ui.webcontrols.webcontrol.cssclass (msdn) . 如果您的用户控件是从webcontrol继承的,则只需使用system.web.ui.webcontrols.webcontrol.cssclass(msdn)即可 Example in the link. 链接中的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM