Add <Script ... /> tag within <Head ... /> tag in ServerControl (ASP.NET)?

Mohammad Dayyan picture Mohammad Dayyan · Nov 7, 2010 · Viewed 14.1k times · Source

I'm gonna use JQuery files in my custom ServerControl , thus I have to add below line within Head tag.

<script type="text/javascript" src="jquery-1.4.3.min.js"></script>

How can I do it in ServerControl with C#

Answer

Strelok picture Strelok · Nov 7, 2010

You can register custom scripts using the ClientScriptManager.RegisterClientScriptInclude Method during the page load. Alternatively, you can just include the script in your .aspx page. If this is a public server control, the first method is probably more preferable.

EDIT: alternatively you can register scripts in the <head> tag of the page as follows:

HtmlGenericControl jqueryInclude = new HtmlGenericControl("script");
jqueryInclude.Attributes.Add("type", "text/javascript");
jqueryInclude.Attributes.Add("src", "http://<path to jQuery>");
Page.Header.Controls.Add(jqueryInclude);