如何从 ascx 控件注册 css 页面?

发布于 2024-09-25 11:29:35 字数 285 浏览 1 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月牙弯弯 2024-10-02 11:29:35

样式必须在 HTML 的 head 部分中定义。这适用于注册外部 CSS 文件的 style 标记和 link 标记。

如果您的页面有一个带有 runat="server" 属性的 head 标记,您可以通过属性 this.Page.Header 以编程方式访问它。

当我需要在开始 和结束 标记之间添加某些内容时,我通常使用的方法就是这样的方法。只需将 url 传递到您的样式表即可。

public void AddStylesheet(string url)
{
    string link = String.Format("<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />", url);
    this.Page.Header.Controls.Add(new LiteralControl { Text = link });
}

Styles must be defined in the head section of your HTML. That goes for both style tags and link tags that register external CSS files.

If your page has a head tag with the runat="server" attribute, you can programmatically access it via the property this.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.

public void AddStylesheet(string url)
{
    string link = String.Format("<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" />", url);
    this.Page.Header.Controls.Add(new LiteralControl { Text = link });
}
尝蛊 2024-10-02 11:29:35

您可以通过三个选项将 CSS 插入到页面中:

  • 外部样式表
  • 内部样式表
  • 内联样式

style 元素必须包含在 head 元素内。如果您尝试设置用户控件中包含的元素的样式,则可以使用这三个选项中的任何一个。需要注意的是(主要是我的观点)内联样式在 99.9% 的情况下都是错误的决定。

一种选择是在您的 Site.Master 的 head 部分中公开一个 ContentPlaceHolder 。然后,在使用用户控件的页面上使用此 ContentPlaceHolder,您将能够放置一个 link 元素,指定用户控件的样式表。

另一种选择是简单地将用户控件的样式规则放入整个站点使用的样式表中。

You have three options to insert CSS into a page:

  • External style sheet
  • Internal style sheet
  • Inline style

The style element must be enclosed within the head 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 this ContentPlaceHolder on pages where you use your user control you'll be able to place a link 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.

夜血缘 2024-10-02 11:29:35
  <style type="text/css">
       @import url(user_stylesheet.css)
       .customClass
        {
            background-color: Lime;
        }

    </style>

在这种情况下,CSS 仅在呈现用户控件时才会呈现。

  <style type="text/css">
       @import url(user_stylesheet.css)
       .customClass
        {
            background-color: Lime;
        }

    </style>

In this case the css will only be rendered when usercontrol rendered.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文