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

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?
 
K

Kevin Spencer

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.
 
S

Scott Allen

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?
 
S

Scott Allen

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.
 
I

id.man9

Sorry, I neglected to mention that the error actually came when
attempting to access the InnerText property.
 
S

Scott Allen

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.
 
A

agapeton

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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top