How to modify <head> tag when using a master page?

  • Thread starter Thread starter ericgla
  • Start date Start date
E

ericgla

I am creating a web app using asp.net 2.0 where all pages are based a
single master page. On some of the aspx pages I need to add javascript
to the head tag in order to use Google maps.

I tried this to access the header:

In the master page:
<HEAD runat="server">

In the aspx page: Page_Load

HtmlHead head = (System.Web.UI.HtmlControls.HtmlHead)Header;

i get this error:

"Cannot get inner content of because the contents are not literal"

Is there a way to programatically add to the head tag from within an
aspx page when using master pages?
 
You have to give the head tag an id, and associate it with a declared
CodeBehind HtmlGeneric Control having the same ID.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but you can't make it stink.
 
HtmlHead head = (System.Web.UI.HtmlControls.HtmlHead)Header;

Header is already a HtmlHead reference, so I can't see how you'd get
the error on this statement.

Are you trying to use the InnerText or InnerHtml properties?
 
You have to give the head tag an id, and associate it with a declared
CodeBehind HtmlGeneric Control having the same ID.

In 2.0 the <head runat="server"> tag is automatically wired to the
Page.Header property.
 
Sorry, I neglected to mention that the error actually came when
attempting to access the InnerText property.
 
The easiest way to modiy the <head> then, when using a master page, is
probably to use a ContentPlaceHolder in the head. Provide default
content for the ContentPlaceHolder in the master page.

If the page using that master page wants to override the head content,
it can specify a content control to overwrite what is there.
 
Actually, according to the W3C (...bow before them) you HAVE to put
style in the head, but you can put script just about anywhere.

So, don't worry about it...

That said, you could put a literal up in the <head></head> masterpage
area and just access that from the content pages...

((Literal)this.Master.FindControl("litStyles")).Text = "<link
rel=\"alternate\" type=\"application/atom+xml\" title=\"WinFX
Harmonics\" href=\"http://www.davidbetz.net/winfx/atom.xml\" />";

Just remember this:
1) Use FindControl
2) Cast it
3) Use it

This technique works in many, MANY places...and is the expert
databinders best friend!

David Betz
http://davidbetz.net/dynamicbliss/
http://davidbetz.net/winfx/
 
Back
Top