Vivian River
Vivian River

Reputation: 32400

problem with relative path to .js file in ASP.net ScriptManager

I'm working with an ASP.net web application.

I've written a user control called LocationSelector that has its own Javascript in an external .js file. In order to load that file, I use the following line of code:

ScriptManager.RegisterClientScriptInclude(this, typeof(LocationSelector), Guid.NewGuid().ToString(), "Controls/LocationSelector.js");

The problem is with "Controls/LocationSelector.js". As long as the page that uses the control is in the root directory of the application, everything works. However, as soon as I try to put this control in a page in a subdirectory, it can't load the Javascript file.

How can I fix this?

Upvotes: 1

Views: 998

Answers (1)

Rozwel
Rozwel

Reputation: 2020

Haven't tested it, but off the top of my head I would say you need something along the lines of this:

ScriptManager.RegisterClientScriptInclude(this, typeof(LocationSelector), Guid.NewGuid().ToString(), Page.ResolveClientUrl("~/Controls/LocationSelector.js")); 

Upvotes: 4

Related Questions