使用RegisterClientScriptBlock在页面末尾注册一个脚本,以避免sys undefined错误

Registering a script at the end of the page using RegisterClientScriptBlock to avoid sys undefined error

本文关键字:脚本 一个 错误 undefined sys RegisterClientScriptBlock 注册 使用      更新时间:2023-09-26

我需要以编程方式调用以下内容,以便在显示模态弹出窗口时设置一些内容。

Sys.Application.add_load(modalSetup);

大致意思

<script type='text/javascript'>
            Sys.Application.add_load(modalSetup);
            function modalSetup() {
                var modalPopup = $find("PopupBehaviorId");
                if (modalPopup != null) {
                    modalPopup.add_shown(SetFocusOnControl);
                }
            }
            function SetFocusOnControl() {
                alert('test');
            }
        </script>

现在的问题是,我有一个script manager在主页。

如果我把这个脚本手动(而不是编程)放在script manager in the master page下面,它将正常工作。

但是当我以编程方式在用户控件中添加模态弹出(我需要)使用ScriptManager.RegisterClientScriptBlock or Page.ClientScript.RegisterClientScriptBlock时,它会抛出著名的sys is undefined错误。

我知道发生了这种情况,因为这个程序性注入在ScriptManager行执行之前将其放入Head section行。

我怎样才能绕过这个。有没有可能在页面的末尾而不是顶部插入这个脚本?

您可以使用ScriptManager的RegisterStartupScript方法