如何在母版页中包含CSS?

发布于 2024-09-29 02:41:36 字数 73 浏览 3 评论 0原文

如何仅在我的 ASP.NET 网站的某些页面中包含 CSS 引用?如果我在母版页中包含引用,则网站的所有页面都会共享 CSS 引用。

How do I include CSS reference in only certain pages on my asp.net website? If I include the reference in my master page, all pages of the website share the CSS reference.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

一生独一 2024-10-06 02:41:36

只需添加一个带有默认值的 CSS ContentPlaceHolder 即可。

基本上,除非您使用子页面中的 标记覆盖该占位符,否则将包含您指定为默认值的 CSS 文件。

您的母版页应如下所示。

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

然后,从使用该母版页的任何页面中,您可以简单地使用不同的样式表覆盖它。

在(示例)AboutUs.aspx

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

Just add a CSS ContentPlaceHolder with a default value in it.

Basically, the CSS file you specify as default will be included unless you override that placeholder with an <asp:Content /> tag from a child page.

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

<asp:Content ID="Content1" ContentPlaceHolderID="Stylesheets" runat="server">
    <link rel="stylesheet" href="/css/form.css" type="text/css" />
</asp:Content>
本宫微胖 2024-10-06 02:41:36

在我的情况下,我在解决方案中的不同位置使用了相同的母版页。由于对我的 css 文件的引用有 ~(波浪号)前缀,我向引用添加了一个 response.write ,如下所示:

<%= ResolveUrl("~/css/myStyle.css") %>

In my situation, i used the same masterpage from different locations in the solution. And since the ~ (Tilde) prefix on the reference to my css files, i added a response.write to the reference like so:

<%= ResolveUrl("~/css/myStyle.css") %>
夏见 2024-10-06 02:41:36

您可以在网站上使用多个母版页。

您还可以使用嵌套母版页。顶层可能具有一般站点结构,然后为每个不同区域提供一个主嵌套母版页。

当您右键单击项目并选择“添加”时,您选择选项“WebContentForm”,而不是“WebForm”。然后您可以选择合适的母版页。

在嵌套母版页中,您将 MasterPageFile 设置为等于顶级母版页。

编辑 当与@Marko的方法结合使用时,您可能会得到以下结果...

这里的优点是所有覆盖只需编写一次。

顶级母版页:

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

无覆盖的嵌套母版页 带有

<%@ Page Language="C#" MasterPageFile="~/Site.master"%>
//don't reference the Stylesheets ContentPlaceHolder and the default is rendered

override.css 的嵌套母版页一

<%@ Page Language="C#" MasterPageFile="~/Site.master"%>
<asp:Content ID="Content1" ContentPlaceHolderID="Stylesheets" runat="server">
    <link rel="stylesheet" href="/css/override.css" type="text/css" />
</asp:Content>

带有 secondaryOverride.css 的嵌套母版页二

<%@ Page Language="C#" MasterPageFile="~/Site.master"%>
<asp:Content ID="Content1" ContentPlaceHolderID="Stylesheets" runat="server">
    <link rel="stylesheet" href="/css/secondOverride.css" type="text/css" />
</asp:Content>

然后,只需在任何 Web 表单上设置适当的母版页即可。

You can use more than one Master page on your site.

You can also use nested master pages. The Top level might have the general site structure, and then one Master nested master page for each of your different areas.

When you right click your project and select Add, you choose the option WebContentForm, instead of WebForm. Then you can select the appropriate masterpage.

In your nested masterpages, you set the MasterPageFile equal to your top level masterpage.

Edit When combined with @Marko's approach you could have the following...

The advantage here is that all of your overrides only have to be written once.

Top Level MasterPage:

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

Nested MasterPage with no override

<%@ Page Language="C#" MasterPageFile="~/Site.master"%>
//don't reference the Stylesheets ContentPlaceHolder and the default is rendered

Nested MasterPage One with override.css

<%@ Page Language="C#" MasterPageFile="~/Site.master"%>
<asp:Content ID="Content1" ContentPlaceHolderID="Stylesheets" runat="server">
    <link rel="stylesheet" href="/css/override.css" type="text/css" />
</asp:Content>

Nested MasterPage Two with secondOverride.css

<%@ Page Language="C#" MasterPageFile="~/Site.master"%>
<asp:Content ID="Content1" ContentPlaceHolderID="Stylesheets" runat="server">
    <link rel="stylesheet" href="/css/secondOverride.css" type="text/css" />
</asp:Content>

Then, just set the appropriate master page on any of your web forms.

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