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. I tried using <%@ Import Namespace="Style.css" but no luck. Thanks for the help.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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 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" rel="nofollow noreferrer noopener" 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/Style.css" rel="nofollow noreferrer noopener" type="text/css" />
</asp:Content>
Method 2
If you want to add a CSS stylesheet to any ASPX page, you should use PlaceHolders.
Master page: (in the section)
<asp:ContentPlaceHolder ID="HeadContent" runat="server"> </asp:ContentPlaceHolder>
ASPX Page:
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> // add your link here </asp:Content>
Method 3
just drag your style sheet to your master page.aspx and it will work for all your other forms
Method 4
You have 3 possible solutions:
- Add your link to the head tag of the master page’s markup. If you do so, all the pages using the given master page will automatically use your css file.
- Use a ContentPlaceHolder in the head tag of your master page. 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.
- You can add your link tag using Javascrip/jQuery functions.
Method 5
Although I went the code behind route below in my Page_Load event (VB), I’ll give the correct answer to A.K as it seemed to answer the question better but just didn’t suit my specific case.
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)
Method 6
Append the link tag to the head using DOM manipulation.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0