The build in Page.ClientScript object allows you to place JavaScript inside an ASP.NET page. We cannot use this object to place JavaScript code or JavaScript include file into the head section of the page.

With ASP.NET 2.0, Visual Studio 2005 automatically places the runat="server" attribute  in the head tag of each page which allows us to interact with the page header.

You may add JavaScript code inside the page header with the following code block:

HtmlGenericControl include = new HtmlGenericControl("script");
include.Attributes.Add("type", "text/javascript");
include.InnerHtml = "alert('Hello World');";
this.Page.Header.Controls.Add(include);

And, you may add JavaScript include file inside the page header with the following code block:

HtmlGenericControl include = new HtmlGenericControl("script");
include.Attributes.Add("type", "text/javascript");
include.Attributes.Add("src", "/jsInclude.js");
this.Page.Header.Controls.Add(include);

Fortunately, Simone B created a library, to include/register scripts and style sheets into the head portion of an ASP.NET page.

HeadScriptManager Class Diagram
HeadScriptManager Class Diagram


In order to use the class library, reference the assembly, create an instance of HeadScriptManager and call its methods.

HeadScriptManager hm = HeadScriptManager.Current;
hm.RegisterHeadScriptResource(typeof(jTip), "jTip.jTip.js");

Source and binaries are available at sourceforge.net.

HeadScriptManager Sample Code
HeadScriptManager Sample Code

 

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Currently rated 4.7 by 3 people

  • Currently 4.666667/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5




Comments

September 3. 2007 14:07

Trackback from DotNetKicks.com

HeadScriptManager - Registering scripts into the page header

DotNetKicks.com

September 7. 2007 18:20

Pingback from mhinze.com

23 Links Today (2007-09-07)

mhinze.com

May 16. 2008 13:49

hai,i am developing a online application.i need some help from u.in my application i have four sections namely aptitude,basic etc.when an user clicks two things should happen 1)the aptitude button i had setted the duration to 30 minutes using javascript function and i am calling the javascript function in page load using attributes method(button aptid.attributes.add("onclick","functionname()");2)the questions should be displayed .the problem is when i write the attributes in the page load event only the jsfunction is executing and the onclick event for the button is not fired .suggest me

hussain

Add comment





(will not be displayed)








  • javascript
  • querystring
  • url parameters
  • parse querystring
  • delayed redirect
  • settimeout
  • focus
  • textbox
  • page load
  • after submit
  • set focus in asp.net 2.0/3.5
  • on page load
  • after postback
  • set control focus
  • postback
  • asp.net 1.x
  • substring
  • substr
  • javascript string methods
  • parsefloat
  • convert strings to numbers
  • parseint
  • javascript history
  • history.go
  • history.back
  • http requests
  • image maps
  • css sprites
  • external css
  • external javascript
  • compress javascript
  • javascript compression
  • ajaxcontroltoolkit
  • tab control
  • array
  • length
  • javascipt
  • lastmodified
  • mstsc
  • terminal services
  • remote desktop connections
  • null
  • undefined
  • array.join
  • string concatenation
  • setinterval
  • clearinterval
  • timing events
  • cleartimeout
  • javascript timing events
  • url redirection
  • location.href
  • location.replace
  • redirect
  • redirection
  • system.io.compression
  • viewstate compression
  • compress viewstate
  • gzipstream
  • loadpagestatefrompersistencemedium
  • savepagestatetopersistencemedium
  • form spam
  • captcha
  • prevent spam without captcha
  • url redirect
  • defaultbutton
  • enter key
  • default button
  • asp.net
  • 2.0
  • form
  • panel
  • 1.1
  • form submit
  • dopostback
  • onkeypress
  • onkeydown
  • onkeyup
  • javascript key events
  • keycode 13
  • disable enter key
  • int32.parse
  • convert.toint32
  • int32.tryparse
  • google
  • hoax
  • gmail
  • storage
  • counter
  • mail
  • visual studio 2005
  • vs 2008
  • copy
  • paste
  • clipboard data
  • static variables
  • application object
  • static property
  • server control
  • web file manager
  • iz web file manager
  • convert
  • parse
  • tryparse
  • file upload control
  • maxrequestlength
  • executiontimeout
  • httpruntime
  • asp.net 2.0
  • registering scripts
  • registerclientscript
  • registerstartupscript
  • cross-browser
  • events
  • improve web site performance
  • compression
  • caching
  • elmah
  • error logging
  • exception
  • error
  • httphandler
  • google sitemap generator
  • sitemap
  • internet information services
  • iis7
  • hosts file
  • localhost
  • windows vista
  • search engine optimization
  • seo
  • search engine friendly pages
  • headscriptmanager
  • class library
  • head
  • css
  • c#
  • iis
  • internet information services manager
  • 401.3 unauthorized
  • 500.0 internal server error
  • http error 500.19
  • google toolbar
  • yellow input fields
  • input
  • select
  • background color
  • mozilla firefox
  • medium trust
  • orcas
  • compileroptions
  • warninglevel
  • zyb
  • mobile phones
  • online backup service
  • online services
  • textarea
  • maxlength
  • limit input length
  • custom server control
  • internal error 2739
  • adobe cs3
  • adobe customer support
  • solution
  • error code 0x80004005
  • 500.19 internal server error
  • meta tags
  • keywords meta tag
  • meta
  • description meta tag
  • fake page rank domains
  • scammers
  • google page rank technology
  • ebay
  • general
  • title
  • page rank