Javascript对象“未定义".ScriptManager.RegisterClientScriptInclude之后出现错误 [英] Javascript object "is not defined" error after ScriptManager.RegisterClientScriptInclude

查看:55
本文介绍了Javascript对象“未定义".ScriptManager.RegisterClientScriptInclude之后出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本上是js对象的外部js文件.我正在使用ScriptManager.RegisterClientScriptInclude方法来包含页面上的文件,然后使用ScriptManager.RegisterClientScriptBlock方法来调用js对象的方法,但是却收到未定义DynamicLoadingPanel"的js错误.我知道方法调用在aspx页面中可用时有效,但是我认为当ScriptManager注册脚本块时,出现了计时错误原因.我目前在Page_Load事件中有寄存器,下面是我的代码.有人可以帮我了解这里发生的事情吗?谢谢.

I have an external js file that is basically a js object. I am using the ScriptManager.RegisterClientScriptInclude method to include the file on page, and after that I am using the ScriptManager.RegisterClientScriptBlock method to call a method of the js object, but am getting a js error of 'DynamicLoadingPanel is not defined'. I know the method call works when I have it in the aspx page, but I think I'm getting the error cause of the timing when the ScriptManager registers the script block. I currently have the Registers in the Page_Load event, below is my code. Can someone please help me understand what's going on here. Thanks.


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.ClientScript.IsClientScriptIncludeRegistered("DynamicLoadingPanel"))
        {
            ScriptManager.RegisterClientScriptInclude(Page, typeof(Page), "DynamicLoadingPanel", "~/dynamicLoadingPanel.js");
            ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "set_LoadingPanelID", "DynamicLoadingPanel.set_loadingPanelID('modalLayer');", true);
        }
    }

推荐答案

经过几次试验和错误,我终于找到了包含外部js文件的正确方法.因此,对于所有遇到此问题的人,我希望这可以为您节省几个令人沮丧的时间.

After a few trial and errors, I finally found the correct way to include an external js file. So for all those that come across this issue, I hope this saves you a couple frustrating hours.

要包含文件,您必须获取当前ScriptManager的对象并将该文件添加到Scripts集合中.下面是一个示例.

To include your file you must get an object of the current ScriptManager and add the file to the Scripts collection. Below is an example.


ScriptManager sm = ScriptManager.GetCurrent(Page);
ScriptReference sr = new ScriptReference("~/scripts/dynamicLoadingPanel.js");
if (!sm.Scripts.Contains(sr))
    sm.Scripts.Add(sr);

这篇关于Javascript对象“未定义".ScriptManager.RegisterClientScriptInclude之后出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆