简体   繁体   中英

Making sense of RegisterStartupScript: How often will the JavaScript code be called?

The documentation of ScriptManager.RegisterStartupScript says (emphasis mine):

Registers a startup script block for every asynchronous postback with the ScriptManager control and adds the script block to the page.

[...]

Remarks

You use this method to register a startup script block that is included every time that an asynchronous postback occurs. [...]

Thus, I would have expected that once I register a script block with RegisterStartupScript, it is executed on every subsequent async postback . However, this is not what happens. Here is a MCVE:

<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
    protected void btnRegisterScript_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Test script",
            "window.alert('Script triggered!');", true);
    }

    protected void btnRegularPostback_Click(object sender, EventArgs e)
    {
        // Do nothing
    }
</script>
<html>
<head>
    <title></title>
</head>
<body>
    <form runat="server">
        <asp:ScriptManager runat="server" />
        <asp:UpdatePanel runat="server">
            <ContentTemplate>
                <asp:Button ID="btnRegisterScript" Text="Register Script" runat="server" OnClick="btnRegisterScript_Click" />
                <asp:Button ID="btnRegularPostback" Text="Regular Postback" runat="server" OnClick="btnRegularPostback_Click" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </form>
</body>
</html>

Repro:

  1. Click on "Register Script".
  2. Click on "Regular Postback".

Expected result:

Both step 1 and step 2 cause a JavaScript alert (because step 1 registered the script and step 2 is an async postback).

Actual result:

Only step 1 causes a JavaScript alert.

This is fine for me. I want the alert only to occur during step 1 (and not step 2). However, the behavior does not match the documentation and, thus, I want to know the reason for my misunderstanding.

I just tried out the scenario you described and this is what i observed: On every click of "Register Script" button, one <script type="text/javascript">window.alert('Script triggered!');</script> tag gets added as child of the 'head' tag. So if you do 3 clicks, you will get 3 script tags under the head tag. I believe this is what the documentation says about every asynchronous postback.

On clicking the "Regular Postback" button, there is no way for the already registered script(registered by "Register Script") in the 'head' tag to execute. And there is no script that is registered in the event handler of this button. So i don't expect the script to get executed in "Regular Postback" button click.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM