Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Insert ScriptBlock in the Head section

44 views
Skip to first unread message

Mike Moore [MS]

unread,
Oct 30, 2002, 10:16:58 PM10/30/02
to
Hi paps,

QUESTION
I need to insert a JS in the Head section when I render a
control but if I use RegisterClientScript it write in the
body section.
The only way I found is to get the head element and then
add the script manually, there is another way ?

ANSWER
I haven't found any ".NET" way to do this, though I will look into it
further tomorrow.

So far, I've found two ways to prepare the code in the code-behind and get
that code to execute from the <head> section.

1)
In the code-behind, inside the "class", but not inside any function,
declare a "protected" string similar to this:
Protected s As String = "<script>a=5</script>"
Then, in the <head> section of your page, add this:
<%=s%>

2)
Use the regular code-behind register functions to create your script.
However, write it as a function, such as:
<script>
function abc() {
....
}
</script>

Then, manually add a call to this function in the <head> section, such as:
<script>
abc();
</script>


---
Both of the above require you to manually insert something into the <head>
section. However, they do allow you to create the body of the script in the
code-behind. This could help if you need to adjust the content of the
script block via the code-behind.

I'll let you know what else (if anything) that I find out tomorrow.

Thank you, Mike Moore
Microsoft, ASP.NET

This posting is provided "AS IS", with no warranties, and confers no rights.

--------------------
>Content-Class: urn:content-classes:message
>From: "ppaps" <pape...@euroforex.com>
>Sender: "ppaps" <pape...@euroforex.com>
>Subject: Insert ScriptBlock in the Head section
>Date: Wed, 30 Oct 2002 06:15:35 -0800
>Lines: 7
>Message-ID: <674a01c2801e$d03fede0$35ef2ecf@TKMSFTNGXA11>
>MIME-Version: 1.0
>Content-Type: text/plain;
> charset="iso-8859-1"
>Content-Transfer-Encoding: 7bit
>X-Newsreader: Microsoft CDO for Windows 2000
>X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
>Thread-Index: AcKAHtA/FfxdCj5MQeqAm65LgMVWvA==
>Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
>NNTP-Posting-Host: TKMSFTNGXA11 10.201.226.39
>Path: cpmsftngxa09!cpmsftngxa08
>Xref: cpmsftngxa09
microsoft.public.dotnet.framework.aspnet.buildingcontrols:3362
>X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingcontrols
>
>I need to insert a JS in the Head section when I render a
>control but if I use RegisterClientScript it write in the
>body section.
>The only way I found is to get the head element and then
>add the script manually, there is another way ?
>
>thanks paps
>

Mike Moore [MS]

unread,
Nov 1, 2002, 9:26:15 PM11/1/02
to
Hi paps,

I'm sorry, but I don't have anything yet. I'll post something for you on
Monday.

Thank you, Mike Moore
Microsoft, ASP.NET

This posting is provided "AS IS", with no warranties, and confers no rights.

--------------------
>X-Tomcat-ID: 97620558
>References: <674a01c2801e$d03fede0$35ef2ecf@TKMSFTNGXA11>
>MIME-Version: 1.0
>Content-Type: text/plain
>Content-Transfer-Encoding: 7bit
>From: mic...@online.microsoft.com ("Mike Moore [MS]")
>Organization: Microsoft
>Date: Thu, 31 Oct 2002 03:16:58 GMT
>Subject: RE: Insert ScriptBlock in the Head section
>X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingcontrols
>Message-ID: <hD5mdvIgCHA.1896@cpmsftngxa09>
>Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
>Lines: 67
>NNTP-Posting-Host: TOMCATIMPORT2 10.201.218.182
>Path: cpmsftngxa08!cpmsftngxa09
>Xref: cpmsftngxa08
microsoft.public.dotnet.framework.aspnet.buildingcontrols:3395

Mike Moore [MS]

unread,
Nov 4, 2002, 6:33:15 PM11/4/02
to
Hi Paps,

QUESTION
I need to insert a JS in the Head section when I render a
control but if I use RegisterClientScript it write in the
body section.
The only way I found is to get the head element and then
add the script manually, there is another way ?

ANSWER
I did not find any way to have the code-behind insert script into the
header section of the HTML output. It always went into the body section. I
think that leaves the following options:

1) Type your script into the ASPX page rather than the code-behind.
This can be done a couple ways. You can type all the script into the HTML,
or you can just type a function call into the HTML (it would have to go in
the onload event so that it doesn't run until after the rest of the page is
rendered).

2) Use RegisterClientScriptBlock which puts the script at the TOP of the
body block rather than in the head block.

Thank you, Mike Moore
Microsoft, ASP.NET

This posting is provided "AS IS", with no warranties, and confers no rights.

--------------------
>X-Tomcat-ID: 51270224
>References: <674a01c2801e$d03fede0$35ef2ecf@TKMSFTNGXA11>
<hD5mdvIgCHA.1896@cpmsftngxa09>


>MIME-Version: 1.0
>Content-Type: text/plain
>Content-Transfer-Encoding: 7bit
>From: mic...@online.microsoft.com ("Mike Moore [MS]")
>Organization: Microsoft

>Date: Sat, 02 Nov 2002 02:26:15 GMT


>Subject: RE: Insert ScriptBlock in the Head section
>X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingcontrols

>Message-ID: <3CYLochgCHA.1548@cpmsftngxa08>
>Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
>Lines: 94
>NNTP-Posting-Host: TOMCATIMPORT2 10.201.218.182
>Path: cpmsftngxa09!cpmsftngxa08
>Xref: cpmsftngxa09
microsoft.public.dotnet.framework.aspnet.buildingcontrols:3383

0 new messages