简体   繁体   English

RegisterStartupScript的含义:会多久调用一次JavaScript代码?

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

The documentation of ScriptManager.RegisterStartupScript says (emphasis mine): ScriptManager.RegisterStartupScript文档说(重点是我):

Registers a startup script block for every asynchronous postback with the ScriptManager control and adds the script block to the page. 使用ScriptManager控件为每个异步回发注册一个启动脚本块,并将该脚本块添加到页面。

[...] [...]

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 . 因此,我期望一旦在RegisterStartupScript中注册了脚本块,便会在随后的每个异步回发中执行该脚本块。 However, this is not what happens. 但是,这不会发生。 Here is a MCVE: 这是一个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). 步骤1和步骤2均会引发JavaScript警报(因为步骤1已注册脚本,而步骤2是异步回发)。

Actual result: 实际结果:

Only step 1 causes a JavaScript alert. 只有步骤1会导致JavaScript警报。

This is fine for me. 这对我很好。 I want the alert only to occur during step 1 (and not step 2). 希望仅在步骤1(而不是步骤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. 我刚刚尝试了您描述的情况,这就是我观察到的情况:每次单击“注册脚本”按钮时,都会出现一个<script type="text/javascript">window.alert('Script triggered!');</script>标签被添加为“ head”标签的子级。 So if you do 3 clicks, you will get 3 script tags under the head tag. 因此,如果您单击3次,将在head标签下获得3个script标签。 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. 单击“常规回发”按钮后,无法执行“ head”标记中已注册的脚本(通过“注册脚本”注册)。 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. 因此,我不希望脚本在“常规回发”按钮单击后执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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