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

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

问题描述

我有一个外部js文件,基本上是一个js对象。我正在使用ScriptManager.RegisterClientScriptInclude方法在页面上包含该文件,之后我使用ScriptManager.RegisterClientScriptBlock方法来调用js对象的方法,但是得到了'DynamicLoadingPanel未定义'的js错误。我知道方法调用在aspx页面中有效时会起作用,但我认为我在ScriptManager注册脚本块时遇到错误原因。我目前在Page_Load事件中有Registers,下面是我的代码。有人可以帮我理解这里发生了什么。谢谢。

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天全站免登陆