473,857 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2050
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 HtmlGenericCont rol 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.RegisterCl ientScriptBlock ().

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.c s]

public class MyPage : Page
{

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

....
}
Thanks.

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

I am sorry if there is any misunderstandin g 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
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
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.m icrosoft.com> wrote in message
news:NL******** ******@cpmsftng xa06.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
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.c s]

public class MyPage : Page
{
....

protected override void Render(HtmlText Writer writer)
{
WriteToHeaderTa g("<script>func tion global(){...</script>");

base.Render (writer);
}
private void WriteToHeaderTa g(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.T oUpper().IndexO f("</HEAD>") > -1)
{
headCtl.Text = InsertTag(headC tl.Text, tag, "</HEAD>");
break;
}

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

anyone has improvements OR a better solution?

Thanks.

"Jacob Yang [MSFT]" <ji***@online.m icrosoft.com> wrote in message
news:NL******** ******@cpmsftng xa06.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
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******** ******@TK2MSFTN GP10.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.c s]

public class MyPage : Page
{
...

protected override void Render(HtmlText Writer writer)
{
WriteToHeaderTa g("<script>func tion global(){...</script>");

base.Render (writer);
}
private void WriteToHeaderTa g(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.T oUpper().IndexO f("</HEAD>") > -1)
{
headCtl.Text = InsertTag(headC tl.Text, tag, "</HEAD>");
break;
}

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

anyone has improvements OR a better solution?

Thanks.

"Jacob Yang [MSFT]" <ji***@online.m icrosoft.com> wrote in message
news:NL******** ******@cpmsftng xa06.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
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******@hotma il.com> wrote in message
news:%2******** ********@tk2msf tngp13.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******** ******@TK2MSFTN GP10.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.c s]

public class MyPage : Page
{
...

protected override void Render(HtmlText Writer writer)
{
WriteToHeaderTa g("<script>func tion global(){...</script>");

base.Render (writer);
}
private void WriteToHeaderTa g(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.T oUpper().IndexO f("</HEAD>") > -1)
{
headCtl.Text = InsertTag(headC tl.Text, tag, "</HEAD>");
break;
}

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

anyone has improvements OR a better solution?

Thanks.

"Jacob Yang [MSFT]" <ji***@online.m icrosoft.com> wrote in message
news:NL******** ******@cpmsftng xa06.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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
18587
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 } Which is called statically by: <button onclick="doClick(event,this);">Click me</button>
3
12495
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 have the following event handler functions defined and need to assign them to table elements (<TD>) created dynamically in another function. function handleMouseOver(elt, cssClass, strURL)
9
39791
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 another "record" the values for all previous controls within the div are wiped out. In the javascript function where I add on to the html in the div if I capture all the data in the previous "records" then after adding the new record I can...
2
1817
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. I'm wondering if there's a way to add the tag from scratch at run time. thanks, Ben
3
11925
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 inherit all my pages from it so the css link below is added to each page automatically in the <head> section.... <LINK href="/MyApp/Css/MyApp.css" type="text/css" rel="stylesheet"> Thanks, Dave.
2
2918
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 avoid the problem by avoiding the table control or resorting to databound controls that better manage state for me. I hope to understand how to solve the problem by using the Table web control and sticking to the approach of building the table at run...
5
3741
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. When the user clicks the "Add another email" it will call a client side JavaScript function, add_email, which will dynamically add a new set of controls to the webpage using the innerHTML method. It appears to work perfectly fine in the browser. The...
2
5199
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 Remove</title> </head>
3
3042
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 line document.getElementById('div1').setAttribute('oncontextmenu', someText); to document.getElementById('div1').setAttribute('oncontextmenu', function(){alert('World World');return false;});
0
9923
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9767
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11082
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10711
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10394
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9549
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5774
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5967
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.