我在名为Scripts的文件夹中编写了一个javascript函数。这个javascript文件中有两个函数用作PopUp。我使用ScriptManager.RegisterStartupScript()从.cs文件中调用了这些javascript函数,并且工作正常。
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "disp_confirm", "<script type='text/javascript'>ImgDivPopup()</script>", false);
但是我把.cs文件放在不同的文件夹中,比如View =&gt; InProcess =&gt; Traveller.cs上面的代码不起作用。
这有可能解决这个问题吗?
答案 0 :(得分:1)
有一些问题可能导致您的现有问题:
确保正确引用
所有RegisterStartupScript()
方法实际上都是在加载所有内容时注册要添加到页面(并经常执行)的特定脚本。当发生这种情况时,您现有的代码将调用ImgDivPopup()
,这通常会假设存在定义该函数的<script>
引用:
<!-- Ensure you are referencing the appropriate file so you can access the function -->
<script src='<%= ResolveUrl("~/Scripts/your-imgdivpopup-file.js") %>'></script>
这只是一个如何使用您定义的函数引用文件的示例,但如果您使用的是外部文件,则需要这样做。
可能的竞争条件
如果您正在引用该文件并且它具有任何其他依赖项,例如另一个客户端库,那么您可以考虑在调用函数时添加显式延迟以便有时间赶上:
// Call your function with a 10ms delay
ScriptManager.RegisterStartupScript(Page, GetType(), "disp_confirm", "setTimeout(function(){ ImgDivPopup();},10);", true);
使用开发人员工具(F12)
由于这可能是客户端问题,请考虑在浏览器中使用开发人员工具(F12)。检查网络和控制台标签,看看是否存在任何错误(例如"function ImgDivPopup() is not defined"
,"{your-script-file} could not be found"
等)。