dynamically adding to HEAD tag

G

Guest

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

developer

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

Jacob Yang [MSFT]

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/magazine/columns/aspnet/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! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.
 
J

Jerry III

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
 
D

developer

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

Jerry III

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 said:
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 said:
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/magazine/columns/aspnet/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! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.
 
D

developer

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 said:
I posted one, why in the hell just don't you use server tags inside
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 said:
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 said:
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/magazine/columns/aspnet/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! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.
 
J

Jacob Yang [MSFT]

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! ¨C www.microsoft.com/security
This posting is provided ¡°as is¡± with no warranties and confers no rights.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top