Header JavaScript Write from Class?

  • Thread starter Thread starter localhost
  • Start date Start date
L

localhost

I have a complex ASPX page, and in the code-behind I instance an
object that inherits from Class. Upon instantiation, I want the
object to write some JavaScript code at the end of the HEAD tag,
immediately before BODY begins.

How can I do that?

Thanks.
 
localhost said:
I have a complex ASPX page, and in the code-behind I instance an
object that inherits from Class. Upon instantiation, I want the
object to write some JavaScript code at the end of the HEAD tag,
immediately before BODY begins.

How can I do that?

Thanks.

Have you tried using Page.RegisterClientScriptBlock()?

ib.
 
Have you tried using Page.RegisterClientScriptBlock()?

ib.

If you absolutely need it in the <HEAD> section, you can try to add an id=
and runat=server attribute to the <HEAD> tag, then in the code-behind,
declare a control variable whose name is the id you just gave the <HEAD>
and declare it as a HtmlGenericControl. Then you can reference the HEAD
tag as a control and alter its inner text, etc.
 
Hi Localhost,

I think Craig's suggestions are resonable and since the ASP.NET doesn't
provide buildin features on editing the Header elements directly,
currently, most ones make use of the Literal control to represent the
<head> area of an asp.net web page.
set the
<head runat="server" id="xxx" >
and then use a LiteralControl in code behind to reference it. Also, if you
want to perform a common framework on handling a group of pages. We still
need to integrate this skill together with the PageControler(base page)
pattern. And below are some certain web resources discussing on the
similiar problem:

#ASP.NET Page Templates - Using Inheritance
http://www.codeproject.com/aspnet/page_templates.asp

#Dynamically Adding <HEAD> Content with ASP.NET
http://dotnetjunkies.com/WebLog/bsblog/archive/2004/02/11/7052.aspx

An improvement to RegisterClientScriptBlock
http://www.codeproject.com/aspnet/scriptregister.asp?print=true

Hope helps. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Thanks for the follow up. I am aware of how to generally write code
in the page header from my code behind class. I am also aware of Page
inheritance and use it a lot.

My problem is that I am generally unsure how to write to the <head> of
a page from within a class that is called by the page itself.

If I have in my code behind:

protected override void Render(HtmlTextWriter htmlTextWriter)
{
base.Render( htmlTextWriter );
htmlTextWriter.Write( "bye." );
}

I want to change this to:

protected override void Render(HtmlTextWriter htmlTextWriter)
{
HeadWriter headWriter = new HeadWriter();
base.Render( htmlTextWriter )
}

And have my headWriter instance "reach back up" to its caller (the
code behind class) and write in the ASPX header at that point.


Thanks.
 
Hi Localhost,

Thanks for the followup. As for the "HeadWriter" function, I'm afraid this
seems that we have to completely implement the Page's structure and render
method ourselves since the ASP.NET Web Page dosn't provide buildin support
on processing head area. I still think the means I mentioned in the former
reply is what we can do currently. We need to use LiteralControl in page
template and provide a certain Collection member in the page class and use
this collection to hold header items and when the page is render out,
output the header items into the LiteralControl in the page's <head> area.
Its hard to make it purely programmatically without any action in page's
html source. Do you think so?


Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Back
Top