最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin6PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far