By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,797 Members | 1,262 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,797 IT Pros & Developers. It's quick & easy.

dynamically adding to HEAD tag

P: n/a
What is the beat way to dynamically write/add to the HEAD
tag of an ASPX page (the <head runat=server ... is too
error prone and not very repeatable)?

Thanks.
Nov 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi,
I'm trying to automate the process of writing <SCRIPT> and <LINK> tags
in-between the HEAD tags (to guarantee that they are processed first by the
browser) in all our ASPX pages.
I need to do this from code-behind code.

I've read some "solutions" such as:
- Making the existing <HEAD> into a HtmlGenericControl by applying a
runat=server
- Adding a PlaceHolder control between the <HEAD> tags.
But these are not suitable for large-scale web apps with many Pages.

I want to extend the Page class, since this would also allow WebControls to
write to the <HEAD> tag, and implement a function similar to the
Page.RegisterClientScriptBlock().

Basically, my question is "How do I write in-between the HEAD tags of the
ASPX page from code-behind code?".
Example:

[in WebForm1.aspx.cs]

public class MyPage : Page
{

protected override void OnLoad(EventArgs e)
{
// Is there any built-in method to write in-between the <HEAD> tags?
}

....
}
Thanks.

"Jacob Yang [MSFT]" <ji***@online.microsoft.com> wrote in message
news:O1**************@cpmsftngxa06.phx.gbl...
Hi,

I am sorry if there is any misunderstanding but I am not sure about your
exact meaning. Would you please provide an example to demonstrate what you
want to do? I certainly appreciate your time.

Best regards,

Jacob Yang
Microsoft Online Partner Support
<MCSD>
Get Secure! 每 www.microsoft.com/security
This posting is provided ※as is§ with no warranties and confers no rights.

Nov 17 '05 #2

P: n/a
Hi,

Based on my research and experience, it seems that it is impossible to
insert client-side script between the <Head> tags with ASP.NET. please
refer to the following URL:

http://www.fawcette.com/vsm/2002_07/...default_pf.asp

It states that:

"It might strike you as odd that ASP.NET inserts client <script> blocks
inside the HTML form instead of within the <head> section that most people
choose for scripts. Microsoft's designers needed to tie the rendering of
the script blocks to a server-side control inside the page, and not to
something that's only literal text. Microsoft chose to put the code inside
the <form> tag rather than make you insert <head runat=server> in your
pages. This is based on the valid assumption that you need a server-side
form if you're using controls. "

Best regards,

Jacob Yang
Microsoft Online Partner Support
<MCSD>
Get Secure! 每 www.microsoft.com/security
This posting is provided ※as is§ with no warranties and confers no rights.

Nov 17 '05 #3

P: n/a
Jacob, that's completely unrelated to this thread. The article you're
quoting simply states that server controls will insert their scripts next to
their HTML instead of in the head element - that makes sense as each control
generates a continuous stream of characters instead of splitting the result
into two pieces and trying to insert one somewhere else in the page. It does
not say that it's impossible to create dynamic HTML inside the head.

Jerry

"Jacob Yang [MSFT]" <ji***@online.microsoft.com> wrote in message
news:NL**************@cpmsftngxa06.phx.gbl...
Hi,

Based on my research and experience, it seems that it is impossible to
insert client-side script between the <Head> tags with ASP.NET. please
refer to the following URL:

http://www.fawcette.com/vsm/2002_07/...default_pf.asp

It states that:

"It might strike you as odd that ASP.NET inserts client <script> blocks
inside the HTML form instead of within the <head> section that most people
choose for scripts. Microsoft's designers needed to tie the rendering of
the script blocks to a server-side control inside the page, and not to
something that's only literal text. Microsoft chose to put the code inside
the <form> tag rather than make you insert <head runat=server> in your
pages. This is based on the valid assumption that you need a server-side
form if you're using controls. "

Best regards,

Jacob Yang
Microsoft Online Partner Support
<MCSD>
Get Secure! 每 www.microsoft.com/security
This posting is provided ※as is§ with no warranties and confers no rights.

Nov 17 '05 #4

P: n/a
Hi,
there are ways, but none are "clean" solutions.
I think the following method is problably the best I've found (w/o having to
implement my own Page factory):

[in WebForm1.aspx.cs]

public class MyPage : Page
{
....

protected override void Render(HtmlTextWriter writer)
{
WriteToHeaderTag("<script>function global(){...</script>");

base.Render (writer);
}
private void WriteToHeaderTag(string tag)
{
LiteralControl headCtl = null;

foreach(Control ctl in this.Controls)
{
if( ctl is LiteralControl)
{
headCtl = (LiteralControl) ctl;

// TODO: handle non existing </HEAD> tag

// write to the head
if (headCtl.Text.ToUpper().IndexOf("</HEAD>") > -1)
{
headCtl.Text = InsertTag(headCtl.Text, tag, "</HEAD>");
break;
}

}
}
}
private string InsertTag(string stringSrc, string stringToAdd, string
stringMarker)
{
return Regex.Replace(stringSrc, stringMarker , stringToAdd +
stringMarker);
}

anyone has improvements OR a better solution?

Thanks.

"Jacob Yang [MSFT]" <ji***@online.microsoft.com> wrote in message
news:NL**************@cpmsftngxa06.phx.gbl...
Hi,

Based on my research and experience, it seems that it is impossible to
insert client-side script between the <Head> tags with ASP.NET. please
refer to the following URL:

http://www.fawcette.com/vsm/2002_07/...default_pf.asp

It states that:

"It might strike you as odd that ASP.NET inserts client <script> blocks
inside the HTML form instead of within the <head> section that most people
choose for scripts. Microsoft's designers needed to tie the rendering of
the script blocks to a server-side control inside the page, and not to
something that's only literal text. Microsoft chose to put the code inside
the <form> tag rather than make you insert <head runat=server> in your
pages. This is based on the valid assumption that you need a server-side
form if you're using controls. "

Best regards,

Jacob Yang
Microsoft Online Partner Support
<MCSD>
Get Secure! 每 www.microsoft.com/security
This posting is provided ※as is§ with no warranties and confers no rights.

Nov 17 '05 #5

P: n/a
I posted one, why in the hell just don't you use server tags inside <head>?
I personally use repeater in there to insert a list of style sheets, what's
so difficult about it? There's absolutely no difference between using server
tags in the body and in the head elements...

Jerry

"developer" <de*******@mgen.com> wrote in message
news:uJ**************@TK2MSFTNGP10.phx.gbl...
Hi,
there are ways, but none are "clean" solutions.
I think the following method is problably the best I've found (w/o having to implement my own Page factory):

[in WebForm1.aspx.cs]

public class MyPage : Page
{
...

protected override void Render(HtmlTextWriter writer)
{
WriteToHeaderTag("<script>function global(){...</script>");

base.Render (writer);
}
private void WriteToHeaderTag(string tag)
{
LiteralControl headCtl = null;

foreach(Control ctl in this.Controls)
{
if( ctl is LiteralControl)
{
headCtl = (LiteralControl) ctl;

// TODO: handle non existing </HEAD> tag

// write to the head
if (headCtl.Text.ToUpper().IndexOf("</HEAD>") > -1)
{
headCtl.Text = InsertTag(headCtl.Text, tag, "</HEAD>");
break;
}

}
}
}
private string InsertTag(string stringSrc, string stringToAdd, string
stringMarker)
{
return Regex.Replace(stringSrc, stringMarker , stringToAdd +
stringMarker);
}

anyone has improvements OR a better solution?

Thanks.

"Jacob Yang [MSFT]" <ji***@online.microsoft.com> wrote in message
news:NL**************@cpmsftngxa06.phx.gbl...
Hi,

Based on my research and experience, it seems that it is impossible to
insert client-side script between the <Head> tags with ASP.NET. please
refer to the following URL:

http://www.fawcette.com/vsm/2002_07/...default_pf.asp
It states that:

"It might strike you as odd that ASP.NET inserts client <script> blocks
inside the HTML form instead of within the <head> section that most people choose for scripts. Microsoft's designers needed to tie the rendering of
the script blocks to a server-side control inside the page, and not to
something that's only literal text. Microsoft chose to put the code inside the <form> tag rather than make you insert <head runat=server> in your
pages. This is based on the valid assumption that you need a server-side
form if you're using controls. "

Best regards,

Jacob Yang
Microsoft Online Partner Support
<MCSD>
Get Secure! 每 www.microsoft.com/security
This posting is provided ※as is§ with no warranties and confers no

rights.


Nov 17 '05 #6

P: n/a
Hi,

we're developing hundreds of pages among multiple developers.

I don't want to maintain theses Pages individually.

I need to cater for the fact that this Control could accidentally be removed
or is misplaced somewhere else OTHER than in the HEAD section.

Each developer must develop his Pages w/o having to remember to add a
Control to the Head.

Templates are only good if everyone uses them all the time AND no one mucks
with the Page HTML (which will happen).

Thanks.

"Jerry III" <je******@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I posted one, why in the hell just don't you use server tags inside <head>? I personally use repeater in there to insert a list of style sheets, what's so difficult about it? There's absolutely no difference between using server tags in the body and in the head elements...

Jerry

"developer" <de*******@mgen.com> wrote in message
news:uJ**************@TK2MSFTNGP10.phx.gbl...
Hi,
there are ways, but none are "clean" solutions.
I think the following method is problably the best I've found (w/o having
to
implement my own Page factory):

[in WebForm1.aspx.cs]

public class MyPage : Page
{
...

protected override void Render(HtmlTextWriter writer)
{
WriteToHeaderTag("<script>function global(){...</script>");

base.Render (writer);
}
private void WriteToHeaderTag(string tag)
{
LiteralControl headCtl = null;

foreach(Control ctl in this.Controls)
{
if( ctl is LiteralControl)
{
headCtl = (LiteralControl) ctl;

// TODO: handle non existing </HEAD> tag

// write to the head
if (headCtl.Text.ToUpper().IndexOf("</HEAD>") > -1)
{
headCtl.Text = InsertTag(headCtl.Text, tag, "</HEAD>");
break;
}

}
}
}
private string InsertTag(string stringSrc, string stringToAdd, string
stringMarker)
{
return Regex.Replace(stringSrc, stringMarker , stringToAdd +
stringMarker);
}

anyone has improvements OR a better solution?

Thanks.

"Jacob Yang [MSFT]" <ji***@online.microsoft.com> wrote in message
news:NL**************@cpmsftngxa06.phx.gbl...
Hi,

Based on my research and experience, it seems that it is impossible to insert client-side script between the <Head> tags with ASP.NET. please
refer to the following URL:

http://www.fawcette.com/vsm/2002_07/...default_pf.asp
It states that:

"It might strike you as odd that ASP.NET inserts client <script> blocks inside the HTML form instead of within the <head> section that most people choose for scripts. Microsoft's designers needed to tie the rendering of the script blocks to a server-side control inside the page, and not to
something that's only literal text. Microsoft chose to put the code inside the <form> tag rather than make you insert <head runat=server> in your
pages. This is based on the valid assumption that you need a server-side form if you're using controls. "

Best regards,

Jacob Yang
Microsoft Online Partner Support
<MCSD>
Get Secure! 每 www.microsoft.com/security
This posting is provided ※as is§ with no warranties and confers no

rights.



Nov 17 '05 #7

P: n/a
Hi Jerry,

I am sorry if there is any misunerstanding. I do not mean it is impossible
to implement the DHTML in <head></head> section. As we have mentioned
before, with the
placeholder control, we can implement what we require in this thread.

The article means that with the build-in asp.net method, we cannot directly
insert any stuff into the section. instead, we should resort to other ways,
for example, using a placeholder as a container...

Best regards,

Jacob Yang
Microsoft Online Partner Support
<MCSD>
Get Secure! 每 www.microsoft.com/security
This posting is provided ※as is§ with no warranties and confers no rights.

Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.