473,320 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

dynamically adding to HEAD tag

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

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

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

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

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

rights.


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

rights.



Nov 17 '05 #7
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.

Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RobG | last post by:
I am trying to dynamically add an onclick to an element, however I just can't get the syntax right. consider the following function: function doClick (evt,x) { // do things with evt and x } ...
3
by: N. Demos | last post by:
How do you dynamically assign a function to an element's event with specific parameters? I know that the code is different for MSIE and Mozilla, and need to know how to do this for both. I...
9
by: Michelle | last post by:
I have a div that is initially empty. Clicking on a button will add some text boxes and other controls so the user can add additional records. In IE all works fine but in Netscape 7.0 when I add...
2
by: brw | last post by:
Is there a way to dynamically add a link tag to the head block of an ..aspx page? I'm aware that you can add a link tag (or literal control) statically and then dynamically modify the attributes....
3
by: Dave | last post by:
Is there a way to dynamically add a reference to the css stylesheet from the codebehind similarly to the script registration features? I was thinking of adding this code to a base class and...
2
by: Chad | last post by:
I have a problem that I am desperate to understand. It involves dynamically adding controls to a Table control that is built as a result of performing a database query. I am not looking to...
5
by: Dennis Fazekas | last post by:
Greetings, I am creating a web form which will all the user to add an unlimited number of email addresses. Basically I have 3 buttons, "Add Another Email", "-" to remove, and a "Save" button....
2
by: Tereska | last post by:
I want to delete script added before. I'm adding script dynamically and i'm removing later. Why it is still working? I have something like this: <html> <head> <title>JS Script...
3
by: ICPooreMan | last post by:
The following is a very simple example of what I want to do take an elements oncontextmenu and changing it dynamically onclick of that same element. The code below will fail unless you change the...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.