如何从 ascx 控件注册 css 页面?
如何在 ascx 控件内注册 css 代码块?
我可以只
<head id="head" runat="server">
<style type="text/css">
.customClass
{
background-color: Lime;
}
</style>
</head>
在 ascx 页面中的任何位置吗?我好像没工作?
How can I register a css code block inside an ascx control?
Can I just have
<head id="head" runat="server">
<style type="text/css">
.customClass
{
background-color: Lime;
}
</style>
</head>
Anywhere in ascx page? I doesn't seem to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
样式必须在 HTML 的
head
部分中定义。这适用于注册外部 CSS 文件的style
标记和link
标记。如果您的页面有一个带有
runat="server"
属性的head
标记,您可以通过属性this.Page.Header
以编程方式访问它。当我需要在开始
和结束
标记之间添加某些内容时,我通常使用的方法就是这样的方法。只需将 url 传递到您的样式表即可。
Styles must be defined in the
head
section of your HTML. That goes for bothstyle
tags andlink
tags that register external CSS files.If your page has a
head
tag with therunat="server"
attribute, you can programmatically access it via the propertythis.Page.Header
.The method I usually use when I need to add something between the opening
<head>
and closing</head>
tag is a method such as this one. Simply pass in the url to your stylesheet.您可以通过三个选项将 CSS 插入到页面中:
style
元素必须包含在head
元素内。如果您尝试设置用户控件中包含的元素的样式,则可以使用这三个选项中的任何一个。需要注意的是(主要是我的观点)内联样式在 99.9% 的情况下都是错误的决定。一种选择是在您的 Site.Master 的 head 部分中公开一个
ContentPlaceHolder
。然后,在使用用户控件的页面上使用此ContentPlaceHolder
,您将能够放置一个link
元素,指定用户控件的样式表。另一种选择是简单地将用户控件的样式规则放入整个站点使用的样式表中。
You have three options to insert CSS into a page:
The
style
element must be enclosed within thehead
element. If you are attempting to style elements contained within a user control you can use any of these three options. As a note (mostly my opinion) inline styling is 99.9% of the time the wrong decision.One option is to expose a
ContentPlaceHolder
in your Site.Master inside the head section. Then using thisContentPlaceHolder
on pages where you use your user control you'll be able to place alink
element specifying a style sheet for your user control.Another option is to simply put the styling rules for your user control in the style sheet used for your entire site.
在这种情况下,CSS 仅在呈现用户控件时才会呈现。
In this case the css will only be rendered when usercontrol rendered.