javascript 如何确保 js 包含在 ascx 文件中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5024736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to ensure js is included in ascx file?
提问by danyloid
So the problem is the following :
所以问题如下:
i do have own user control. which has some javascript file in script tag included like this
我确实有自己的用户控制。它在脚本标签中有一些 javascript 文件,包括这样
<script type="text/javascript" src="../somefile.js" ></script>
or some javascript code directly on the ascx page. everything works if i place this control somewhere in aspx markup. but if i add this control to the page dynamically to some UpdatePanel place holder on postback (this is logic i can not alter), control renders itself and then i get js error message, which says that functions which are placed in somefile.js are not defined/are null. Why is this happening ? Is there anyway to force js including in this case.
或直接在 ascx 页面上的一些 javascript 代码。如果我将此控件放置在 aspx 标记中的某个位置,则一切正常。但是如果我在回发时将此控件动态添加到某些 UpdatePanel 占位符中(这是我无法更改的逻辑),则控件会呈现自身,然后我收到 js 错误消息,其中说明放置在 somefile.js 中的函数是未定义/为空。为什么会这样?在这种情况下,是否有任何强制 js 包括在内。
sorry for being not enough specific but the code amount is huge and i was not able to provide a simplified example with the same error,.
抱歉不够具体,但代码量很大,我无法提供具有相同错误的简化示例。
The script is included but somehow the functions are not defined. I'm novice to js, so is it possible that just the script is included but not executed, so the functions are not declared ???
该脚本包含在内,但不知何故未定义函数。我是 js 的新手,所以有可能只包含脚本但不执行,所以没有声明函数???
The interesting is that if on some page my custom control is declared in aspx. The adding some more instances dynamically causes no problem.
有趣的是,如果在某个页面上我的自定义控件是在 aspx 中声明的。动态添加更多实例不会导致问题。
回答by Jonathan Wood
In the load event of your control, do something like this:
在您的控件的加载事件中,执行如下操作:
if (!Page.ClientScript.IsClientScriptIncludeRegistered("keyName"))
Page.ClientScript.RegisterClientScriptInclude("keyName",
Page.ResolveUrl("~/Scripts/MyJavaScript.js"));
回答by Bala R
How about including it in ScriptManager.Scripts like this?
像这样将它包含在 ScriptManager.Scripts 中怎么样?
<asp:ScriptManager runat="server" ID="scriptManager1" EnablePageMethods="true">
<Scripts>
<asp:ScriptReference Path="~/somefile.js" />
</Scripts>
</asp:ScriptManager>
Edit:
编辑:
From ascx you could do something like this (OnPreRender would be where I would try this):
从 ascx 你可以做这样的事情(OnPreRender 将是我尝试这样做的地方):
ScriptManager.GetCurrent(Page).Scripts.Add(new ScriptReference("~/somefile.js"));
Alternatively, you could try
或者,您可以尝试
ScriptManager.GetCurrent(Page).RegisterClientScriptInclude(
this,
typeof(Page),
"UniqueScriptKey",
ResolveClientUrl("~/somefile.js"));
回答by Rob
Are you sure your JS file is loading? It could be that the file is not in the right location - don't use ../ in the path as this is relative and may be wrong if you are sharing this and the pages are not all at the same depth:
你确定你的 JS 文件正在加载?可能是文件不在正确的位置 - 不要在路径中使用 ../ ,因为这是相对的,如果您正在共享它并且页面的深度不同,则可能是错误的:
do something like this:
做这样的事情:
<script type="text/javascript" src="/Scripts/somefile.js" ></script>

