use*_*399 3 javascript c# scriptmanager
我有一个控件,在更新面板上.我希望每次更新updatePAnel时都运行我的javascript代码.
我使用这样的东西:
ScriptManager.RegisterStartupScript(this, GetType(), "my_script", "runFunction();", true);
Run Code Online (Sandbox Code Playgroud)
问题是我的js代码很大,我想将它放在js文件中并从文件中使用它.我的代码应该改变什么?
Rap*_*ael 11
你可以使用这个ScriptManager.RegisterClientScriptInclude方法:
ScriptManager.RegisterClientScriptInclude(
updatePanel,
updatePanel.GetType(),
"a_key",
"myScript.js"
);
Run Code Online (Sandbox Code Playgroud)
请注意,此方法将在HTML中尽早呈现您的脚本,因此您的脚本不应该依赖于在页面上呈现的顺序脚本.
有关此方法的更多信息,请访问http://msdn.microsoft.com/pt-br/library/bb337005.aspx
但是,如果您的脚本依赖于其他脚本,则更好的选择是使用该ScriptManager.RegisterStartupScript方法,但不是将脚本体作为参数传递,而是使用<script>脚本的地址传递整个标记:
ScriptManager.RegisterStartupScript(
updatePanel,
updatePanel.GetType(),
"a_key",
"<script type='text/javascript' src='my_script.js'></script>",
false
);
Run Code Online (Sandbox Code Playgroud)
请注意,设置addScriptTags标志的最后一个参数设置为false,允许您使用src定义的属性呈现整个标记.