DotnetDude
DotnetDude

Reputation: 11817

JS path in User control

The folder structure we have is something like this:

ParentFolder
   HostPage1.aspx
   UserControlsFolder
      UserControl.ascx
   AnotherFolder
      HostPage2.aspx

UserControl.ascx is used in both HostPage1.aspx AND HostPage2.aspx

I am including an external JS file in the ASCX and the path needs to be relative to the Page. Since I have pages in different hierarchy structure, how would I conditionally change the path based on whether its being used in HostPage1 or HostPage2?

If it can be avoided, I would not want to add the JS include directive in the hostpages

EDIT Can I use web resource in a user control?

Upvotes: 0

Views: 351

Answers (2)

Ian Robinson
Ian Robinson

Reputation: 16944

I'd recommend using the RegisterClientScriptResource utility. You can certainly do this in a user control.

  1. Right click on the JavaScript file and click properties.
  2. Set Build Action to Embedded Resource
  3. Modify the code below and add it to your user control.
Page.ClientScript.RegisterClientScriptResource(typeof(CurrentTypeHere), "Your.Namespace.Class.Folder.File.js");

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 416049

You could use a web resource

Upvotes: 0

Related Questions