代码之家  ›  专栏  ›  技术社区  ›  Matt

如何从ascx控件注册css页?

  •  3
  • Matt  · 技术社区  · 15 年前

    <head id="head" runat="server">
        <style type="text/css">
            .customClass
            {
            background-color: Lime;
            }
    
            </style>
    </head>
    

    在ascx页面的任何地方?我好像不工作?

    3 回复  |  直到 15 年前
        1
  •  5
  •   ahsteele tungi52    15 年前

    你有 three options to insert CSS 进入页面:

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

    这个 style 元素必须包含在 head

    一种选择是公开 ContentPlaceHolder 在你的站点.主头部内部。然后用这个 内容占位符 link 元素指定用户控件的样式表。

        2
  •  8
  •   wsanville    15 年前

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

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

    我通常使用的方法,当我需要添加一些东西之间的开幕式 <head> </head> tag是这样一个方法。只需将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 });
    }
    
        3
  •  0
  •   Abdul Saboor    13 年前
      <style type="text/css">
           @import url(user_stylesheet.css)
           .customClass
            {
                background-color: Lime;
            }
    
        </style>
    

    在这种情况下,css将仅在usercontrol呈现时呈现。