简体   繁体   English

使用母版页在Aspx页面中引用CSS工作表

[英]Referencing CSS Sheet in Aspx page with Master Page

Question is pretty well stated in the title. 问题在标题中很清楚。 Normally I would use <link... /> to reference my CSS Sheet but since I'm using a master page I don't have access to the Head Tag so how do I reference a specific CSS sheet on my ASPX page. 通常我会使用<link... />来引用我的CSS Sheet,但由于我使用的是母版页,我无法访问Head标签,因此如何在ASPX页面上引用特定的CSS表格。 I tried using <%@ Import Namespace="Style.css" but no luck. 我尝试使用<%@ Import Namespace="Style.css"但没有运气。 Thanks for the help. 谢谢您的帮助。

Just add a CSS ContentPlaceHolder with a default value in it. 只需添加一个CSS ContentPlaceHolder,其中包含默认值。

Basically, the CSS file you specify as default will be included unless you override that placeholder with an tag from a child page. 基本上,除非您使用子页面中的标记覆盖该占位符,否则将包含您指定为默认值的CSS文件。

Your Master Page should look something like this. 您的母版页看起来应该是这样的。

<head>
    <asp:ContentPlaceHolder ID="Stylesheets" runat="server">
        <link rel="stylesheet" href="/css/master.css" type="text/css" />
    </asp:ContentPlaceHolder>
</head>

Then from any pages using that Master Page, you can simply override that with a different stylesheet. 然后,从使用该母版页的任何页面,您只需使用不同的样式表覆盖它。

On (example) AboutUs.aspx 在(例子)AboutUs.aspx

<asp:Content ID="Content1" ContentPlaceHolderID="Stylesheets" runat="server">
    <link rel="stylesheet" href="/css/Style.css" type="text/css" />
</asp:Content>

If you want to add a CSS stylesheet to any ASPX page, you should use PlaceHolders. 如果要将CSS样式表添加到任何ASPX页面,则应使用PlaceHolders。

Master page: (in the section) 母版页:(在本节中)

<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>

ASPX Page: ASPX页面:

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
// add your link here
</asp:Content>

只需将样式表拖到主页面.aspx,它将适用于所有其他表单

You have 3 possible solutions: 您有3种可能的解决方案:

  1. Add your link to the head tag of the master page's markup. 将链接添加到母版页标记的head标记。 If you do so, all the pages using the given master page will automatically use your css file. 如果这样做,使用给定母版页的所有页面将自动使用您的css文件。

  2. Use a ContentPlaceHolder in the head tag of your master page. 在母版页的head标签中使用ContentPlaceHolder。 You can use the ContentPlaceHolder at your pages referencing the given master page and you can add your link tag inside the ContentPlaceHolder tag in the markup. 您可以在引用给定母版页的页面上使用ContentPlaceHolder,并且可以在标记中的ContentPlaceHolder标记内添加链接标记。

  3. You can add your link tag using Javascrip/jQuery functions. 您可以使用Javascrip / jQuery函数添加链接标记。

Although I went the code behind route below in my Page_Load event (VB), I'll give the correct answer to AK as it seemed to answer the question better but just didn't suit my specific case. 虽然我在我的Page_Load事件(VB)中使用了下面的路径代码,但我会给出AK的正确答案,因为它似乎更好地回答了这个问题但是不适合我的具体情况。

Dim link As New HtmlLink()
link.Attributes.Add("href", Page.ResolveClientUrl("../Css/Generic-Form2.css"))
link.Attributes.Add("Type", "text/css")
link.Attributes.Add("rel", "stylesheet")
Page.Header.Controls.Add(link)

使用DOM操作将link标记附加到head

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

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