Click here to Skip to main content
15,883,883 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 :

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


I wanna know how I should write path to jQuery in above code.
Would you please guide me ?
Thanks.
Posted
Updated 7-Nov-10 8:38am
v2
Comments
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");
this.Page.Header.Controls.Add(include); 


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
 
Comments
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: http://127.0.0.1/
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: http://127.0.0.1/WebSite1/jquery-1.4.3.min.js
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.

C#
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.

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

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");
this.Page.Header.Controls.Add(include);
 
Share this answer
 
Comments
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