Search code examples
c#asp.netjqueryservercontrolsservercontrol

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


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#


Solution

  • 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);