How to retain script block on a partial postback?

岁酱吖の 提交于 2019-12-05 05:41:57

For your script block to properly support partial page rendering, you have to register it during the PreRender phase of your user control, using the ScriptManager.RegisterClientScriptBlock() method:

protected void Page_PreRender(object sender, EventArgs e)
{
    if (ShowAlertScript) {
        ScriptManager.RegisterClientScriptBlock(this,
            typeof(YourUserControl), "AlertScript",
            @"function AlertMe() 
              {
                  alert('Hello World!');
              }",
            true);
    }
}

(The above assumes you have the AutoEventWireup attribute set to true in the @ Control directive of your ascx file.)

Also, since the script is registered from a user control but your ScriptManager resides on the page itself, you'll probably have to add an <asp:ScriptManagerProxy> element to your control markup for the above to work.

UpdatePanels actually have their content changed by the AJAX framework setting innerHTML on the div corresponding to the UpdatePanel control.

Script elements are not executed when innerHTML is set.

Therefore, there is no way to have inline (i.e. declarative) JS executed during a partial postback.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!