3

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#

4
  • You have to add your .js file in Solution Explorer Commented Nov 7, 2010 at 12:36
  • I've already done it, but the Script tag doesn't add within Head tag !!! Commented Nov 7, 2010 at 12:40
  • You have to add it manually into your ASPX file, or register it from your code. See my answer. Commented Nov 7, 2010 at 12:42
  • 1
    I've updated my answer. Check it out. Commented Nov 7, 2010 at 13:19

1 Answer 1

11

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);
Sign up to request clarification or add additional context in comments.

3 Comments

I've tested it, It adds the Script tag within the Form tag, I wanna add it within Head tag !!!
Awesome, Thanks dude, But one question, How should I write the Path of file? I save the file in JavaScriptFiles folder in ServerControl Solution !!!
This doesn't add to the head

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.