最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Registering a script at the end of the page using RegisterClientScriptBlock to avoid sys undefined error - Stack Ov

programmeradmin8浏览0评论

I need to call the following programatically to set something up when a modal popup is shown.

Sys.Application.add_load(modalSetup);

Something along the lines.

<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>

Now the problem is that I have a script manager on the master page.

If I put this script manually (not programatically) below the script manager in the master page, it will work correctly.

But when I'm adding this programatically in the user control for modal popup (which I need to) using either ScriptManager.RegisterClientScriptBlock or Page.ClientScript.RegisterClientScriptBlock, it throws the famous sys is undefined error.

I know this is happening because this programatic injection puts it into the Head section before the ScriptManager line is executed.

How can I get around this. Is it possible to say insert this script at the end of the page and not at the top?

I need to call the following programatically to set something up when a modal popup is shown.

Sys.Application.add_load(modalSetup);

Something along the lines.

<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>

Now the problem is that I have a script manager on the master page.

If I put this script manually (not programatically) below the script manager in the master page, it will work correctly.

But when I'm adding this programatically in the user control for modal popup (which I need to) using either ScriptManager.RegisterClientScriptBlock or Page.ClientScript.RegisterClientScriptBlock, it throws the famous sys is undefined error.

I know this is happening because this programatic injection puts it into the Head section before the ScriptManager line is executed.

How can I get around this. Is it possible to say insert this script at the end of the page and not at the top?

Share Improve this question edited Mar 29, 2021 at 8:13 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 11, 2011 at 22:27 Alex JAlex J 1,5673 gold badges26 silver badges42 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5 +100

You can use ScriptManager's RegisterStartupScript method.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论