Is it possible to add javascript reference dynamically from code behind aspx.cs?
是否可以从aspx.cs后面的代码动态添加javascript引用?
Like this:
是这样的:
private void AddScriptReference(string path)
{
//Add reference to <head></head>
}
Should result in a script reference being added to the head of the page, like this:
应该将脚本引用添加到页面的头部,如下所示:
<html>
<head>
<script type="text/javascript" src="path-to-script.js"></script>
</head>
</html>
Is this possible?
这是可能的吗?
3 个解决方案
#1
6
You can use the ASP.NET Ajax ScriptManager
to do so.
您可以使用ASP。NET Ajax ScriptManager这样做。
Add it to your masterpage, and use ScriptManager.RegisterClientScriptInclude
from your codebehind.
将它添加到主页,并使用ScriptManager。RegisterClientScriptInclude后台代码。
#2
26
Bit late but thought I'd post an answer to this in case anybody else needs it. This solution negates the need for a ScriptManager.
有点晚了,但我想我应该发布一个答案,以防有人需要它。这个解决方案否定了使用ScriptManager的需要。
Basically, it's just a case of creating a control and then adding to the head. Here's the code.
基本上,这只是一个创建控件然后添加到头部的例子。这里的代码。
LiteralControl javascriptRef = new LiteralControl("<script type='text/javascript' src='path_to_script.js'></script>");
Page.Header.Controls.Add(javascriptRef);
#3
8
For those who want to know the syntax, here it is:
对于那些想了解语法的人,以下是:
Master Page:
主页:
<asp:ScriptManager ID="ScriptManager" EnablePageMethods="true" runat="server"></asp:ScriptManager>
Code behind:
背后的代码:
ScriptReference sr = new ScriptReference("path-to-js.js");
ScriptManager sm = (ScriptManager)this.Master.FindControl("ScriptManager");
sm.Scripts.Add(sr);
Or:
或者:
ScriptManager.RegisterClientScriptInclude(this.Page, GetType(), "UniqueID", "path-to-js.js");
But none of these solutions actually add the script to the head of the page..
但是,这些解决方案都没有将脚本添加到页面的头部。
#1
6
You can use the ASP.NET Ajax ScriptManager
to do so.
您可以使用ASP。NET Ajax ScriptManager这样做。
Add it to your masterpage, and use ScriptManager.RegisterClientScriptInclude
from your codebehind.
将它添加到主页,并使用ScriptManager。RegisterClientScriptInclude后台代码。
#2
26
Bit late but thought I'd post an answer to this in case anybody else needs it. This solution negates the need for a ScriptManager.
有点晚了,但我想我应该发布一个答案,以防有人需要它。这个解决方案否定了使用ScriptManager的需要。
Basically, it's just a case of creating a control and then adding to the head. Here's the code.
基本上,这只是一个创建控件然后添加到头部的例子。这里的代码。
LiteralControl javascriptRef = new LiteralControl("<script type='text/javascript' src='path_to_script.js'></script>");
Page.Header.Controls.Add(javascriptRef);
#3
8
For those who want to know the syntax, here it is:
对于那些想了解语法的人,以下是:
Master Page:
主页:
<asp:ScriptManager ID="ScriptManager" EnablePageMethods="true" runat="server"></asp:ScriptManager>
Code behind:
背后的代码:
ScriptReference sr = new ScriptReference("path-to-js.js");
ScriptManager sm = (ScriptManager)this.Master.FindControl("ScriptManager");
sm.Scripts.Add(sr);
Or:
或者:
ScriptManager.RegisterClientScriptInclude(this.Page, GetType(), "UniqueID", "path-to-js.js");
But none of these solutions actually add the script to the head of the page..
但是,这些解决方案都没有将脚本添加到页面的头部。