How to add Script in the Page <head></head> dynamically

GibboK picture GibboK · Aug 3, 2011 · Viewed 15.9k times · Source

I use asp.net 4 and c#.

I have a Web User Control inside a Web From page. When I include the Web User Control I would like also include programmatically some script within the tags for the final generated page.

Any idea how to do it? Maybe ScriptManager could help on this?

Please provide me a sample of code I'm pretty new at developing thanks for your time on this!

Answer

joelmdev picture joelmdev · Aug 3, 2011

Peter's answer will add to the page, but not the head element. Take a look at this article:

http://weblogs.asp.net/johnkatsiotis/archive/2008/08/08/add-scripts-to-head-dynamically.aspx

It provides a nice clean way to add a script to the head element using an extension method.