How to Link Stylesheets from a Master Page (Part 2)
Posted in Development
The omnipresent Scott Guthrie has descended upon this blog to set the record straight: the header control was ordained with magical powers of URL rebasing in pre-existence! If you pull out your dusty magnifying glass and pore over Scott’s comment, note this key phrase: relative to the master page location. I guess no need to use my property hack in the head, although it comes in handy in the rest of the master page.
And then Scott Allen steals the thunder again: what if you put a ContentPlaceHolder right in the head? Good idea.
<asp:ContentPlaceHolder id="AdditionalHeaderEntries" runat="server"> </asp:ContentPlaceHolder>
If I need to declare page-scope CSS rules or link to an external JavaScript file, I can do it selectively on every page that needs extra beef:
<asp:Content ContentPlaceHolderID="AdditionalHeaderEntries" runat="server"> <style type="text/css" media="all"> h1 { text-align: center; line-height: 1.3em; margin-top: 0.5em; } </style> </asp:Content>
Many thanks to Scott and Scott for the discussion.
5 comments
Michael Schøler
on July 12, 2007
And why is this possible in the
tag without any problems?<% Response.Write(this.sHeadTitle); %>
Michael Schøler
on July 12, 2007
HTML formatted comment-code is stripped by this blog apparently, I wonder how you other guys got your code through?...
Oh well, you can insert Response.Write inside the title head section without any problems. Odd.
Milan Negovan
on July 12, 2007
Michael, Response.Write produces a literal string, so the runtime won't see it, AFAIK. Also, it won't touch the head element. The issue is with rebasing URLs, i.e. external JavaScript and CSS files.
Clinton Gallagher
on December 9, 2008
If only Scott Gu would show up --anywhere-- to instruct how to work-around the nightmare he created by undermining the use of MasterPages and Themes that disallow changing CSS stylesheets to respond to all of the various browser instances and the rendering problems each browser imposes.
He has apparently turned his back on this matter taking no responsibility for the havoc he and his crew have caused.

kl
on July 15, 2006
Yes, I've used ContentPlaceHolder as well. The markup editor complains about it but it works. I hope that ContentPlaceHolders will continue to be valid in the HEAD in future.