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

Viewed 91780
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

5 Answers

In case Page seems inaccessible from the class you are in.

Page page = HttpContext.Current.CurrentHandler as Page;
        

ScriptManager.RegisterStartupScript(page,page.GetType(), Guid.NewGuid().ToString(), yourScriptVariable, true);
Related