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.