RegisterStartupScript doesn’t work with ScriptManager,Updatepanel. Why is that

asp.netscriptmanagerupdatepanel

protected void timer1_Tick(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in rpChat.Items)
        {
            TextBox txt = item.FindControl("txtChatMessage") as TextBox;
            if (txt != null)
            {
                message[i] = txt.Text;
                i--;
            }
        }
        lblStatusChat.Text = "";
        RepeaterBind();
        string javaScript = "<script language=JavaScript>\n" + "alert('Button1_Click client-side');\n" + "</script>";

        Page.ClientScript.RegisterStartupScript(this.GetType(), "myKey", javaScript);
    }

timer_click trigggers and update panel. And the alert message doesnt show up on timer_tick event

Best Answer

When you use an UpdatePanel, then you can not call JavaScript using ClientScript as you have tried to. You have to use ScriptManager.RegisterStartupScript instead.

So change your

Page.ClientScript.RegisterStartupScript(this.GetType(), "myKey", javaScript);

to

ScriptManager.RegisterStartupScript(updatePanelId,updatePanelId.GetType(), "alert", javaScript, true);
Related Topic