How to use external js file in ScriptManager.RegisterStartupScript?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 20:06:50

问题


i have a control which is on update panel. i want my javascript code run every time the updatePAnel is updated.

i use something like this:

ScriptManager.RegisterStartupScript(this, GetType(), "my_script", "runFunction();", true);

the problem is that my js code is huge and i want to place it in js file and use it from file. what should i change in my code ?


回答1:


You could use the ScriptManager.RegisterClientScriptInclude method:

ScriptManager.RegisterClientScriptInclude(
    updatePanel,
    updatePanel.GetType(),
    "a_key",
    "myScript.js"
);

Note that this method will render your script early in the HTML, so your script should not rely on the order scripts are rendered on the page.

More about this method at http://msdn.microsoft.com/pt-br/library/bb337005.aspx

But if your script depends on some other script, a better option is to use the ScriptManager.RegisterStartupScript method, but instead of passing the script body as a parameter, you pass the entire <script> tag with your script's address:

ScriptManager.RegisterStartupScript(
    updatePanel,
    updatePanel.GetType(),
    "a_key",
    "<script type='text/javascript' src='my_script.js'></script>",
    false
);

Note that the last parameter, which sets the addScriptTags flag, is set to false, allowing you to render the entire tag with the src attribute defined.



来源:https://stackoverflow.com/questions/9930352/how-to-use-external-js-file-in-scriptmanager-registerstartupscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!