我需要添加特定的js文件到页面。 在Page_Load
,我想这样的:ASP.NET在页面加载时添加js文件
ClientScript.RegisterClientScriptInclude("MyTab", HttpRuntime.AppDomainAppPath + "\\scripts\\" + tabName);
它不工作。
我需要添加特定的js文件到页面。 在Page_Load
,我想这样的:ASP.NET在页面加载时添加js文件
ClientScript.RegisterClientScriptInclude("MyTab", HttpRuntime.AppDomainAppPath + "\\scripts\\" + tabName);
它不工作。
您可以尝试此解决方案,将始终工作。使用:
Page.Header.Controls.Add(new LiteralControl("<script type='text/javascript' src='script.js'></script>"));
你可以简单地做到这一点,而无需将其加载到后台代码:
<asp:ScriptManager ID="sm" runat="server">
<Scripts>
<asp:ScriptReference Path="./script.js" />
</Scripts>
</asp:ScriptManager>
如果要添加或更改在运行该脚本文件,刚刚离开的ScriptManager在加价并像这样访问它:
ScriptManager sm = ScriptManager.GetCurrent(Page);
if (Smgr != null)
{
ScriptReference sr = new ScriptReference();
sr.Path = "~/Scripts/Script.js";
sm.Scripts.Add(sr);
}
确保您没有在其他地方使用"MyTab"
注册脚本。这是脚本的a key。
另外HttpRuntime.AppDomainAppPath
将返回the physical path,这使我认为它可能会返回例如C:\Program Files\...
这将不适用于访问该网站的人。
也许尝试:
ClientScript.RegisterClientScriptInclude("MyTab", Page.ResolveClientUrl("~\\scripts\\" + tabName));