0

I need to add specific js files to the page. On Page_Load, I'm trying this:

ClientScript.RegisterClientScriptInclude("MyTab", HttpRuntime.AppDomainAppPath + "\\scripts\\" + tabName);

It doesn't working.

0

3 Answers 3

2

You can try this solution that will always work. use:

Page.Header.Controls.Add(new LiteralControl("<script type='text/javascript' src='script.js'></script>"));
Sign up to request clarification or add additional context in comments.

Comments

0

You can simply do this without the need to load it in the code-behind:

 <asp:ScriptManager ID="sm" runat="server">
   <Scripts>
     <asp:ScriptReference Path="./script.js" />
   </Scripts>
</asp:ScriptManager> 

If you want to add or change the script file at runtime, just leave the ScriptManager in your mark-up and access it like this:

   ScriptManager sm = ScriptManager.GetCurrent(Page);
   if (Smgr != null) 
   {
     ScriptReference sr = new ScriptReference();
     sr.Path = "~/Scripts/Script.js";
     sm.Scripts.Add(sr);
   }

Comments

0

Make sure you're not using "MyTab" anywhere else to register scripts. It's a key for the script.

Also HttpRuntime.AppDomainAppPath will return the physical path, which makes me think it could return for example C:\Program Files\... which won't work for people visiting the site.

Maybe try:

ClientScript.RegisterClientScriptInclude("MyTab", Page.ResolveClientUrl("~\\scripts\\" + tabName));

Comments

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.