Click here to Skip to main content
15,850,299 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I'm using jquery-1.4.3.min.js in my custom ServerControl, I've inserted the file into the JavaScriptFiles folder in my project solution, thus we can find jquery-1.4.3.min.js in the below path :
My Project Solution >> JavaScriptFiles >> jquery-1.4.3.min.js

Then I've added a Script tag into the Page header with the following code :

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

I wanna know how I should write path to jQuery in above code.
Would you please guide me ?
Updated 7-Nov-10 9:38am
Venkatesh Mookkan 8-Nov-10 7:19am    
You are suppose to Vote the right (helped) answers below

Easiest would be a relative path. In the code below the script is located in the same path:
HtmlGenericControl include = new HtmlGenericControl("script");
include.Attributes.Add("type", "text/javascript");
include.Attributes.Add("src", "/jquery-1.4.3.min.js");

If it is located in a sub directory you could do something like: "/scripts/jquery-1.4.3.min.js"

You could also test it by entering your url directly in your browser and if that is correct, you will see the script returned as text or you get the option to download it.

Good luck!
Share this answer
Mohammad Dayyan 7-Nov-10 14:41pm    
Thanks friend , I've tested all of options :-( but no one work. Actually I'm working on ServerControl, and I have to add Script tag in each Web Application Page that is using my ServerControl.
E.F. Nijboer 8-Nov-10 8:03am    
But did you test the url to the script file using a browser? This way you can check if you can reach it yourself. You take the initial address of your page, like:
Then get the path where the aspx is located that includes the script, like: WebSite1/test.aspx
Let's assume that the jquery-1.4.3.min.js file is located in the same directory.
You get URL:
If you would place the constructed URL in the browser (while debugging if you work from within visual studio) you should get the script as text or download, otherwise the URL is incorrect or the file is missing and should be fixed. So, first locate the file by hand in the browser. If that doesn't work there is something wrong.
You could try forming full URL using HttpRequest object.

Uri server = HttpContext.Current.Request.Url;
string uriBase = string.Format("{0}://{1}:{2}", server.Scheme, server.Host, server.Port);

Here uriBase gives you the url base for your website concatenate the JavaScript file location along with it.

Please check the source (HTML) in the IE whether you are referencing the JavaScript more than once.
Share this answer
Mohammad Dayyan 8-Nov-10 6:15am    
How should I do it ? I'm newbie
Venkatesh Mookkan 8-Nov-10 6:29am    
Answer updated.

Try include the ResolveUrl method. See below.

HtmlGenericControl include = new HtmlGenericControl("script");
include.Attributes.Add("type", "text/javascript");
include.Attributes.Add("src", ResolveUrl("~/") + "JavaScriptFiles/jquery-1.4.3.min.js");
Share this answer
Mohammad Dayyan 8-Nov-10 6:17am    
It doesn't work again :-( I inserted `jquery-1.4.3.min.js` in the ServerControl Project, Should I inserted the file in a Web App that is used my ServerControl ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900