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

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);

You need to user ScriptManager class because you are register script when doing postback and using updatepanel

MSDN: ScriptManager.RegisterStartupScript

ScriptManager.RegisterStartupScript method used to add client script to a page when the control is wrapped inside an UpdatePanel.

ASPX page

<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Label ID="lblDisplayDate" runat="server" Text="Label"></asp:Label>
         <asp:Button ID="btnPostback" runat="server" onclick="btnPostback_Click" 
        Text="ClickMe" />
    </ContentTemplate>
</asp:UpdatePanel>
</div>

CodeBehind Register StartUp Script

protected void btnPostback_Click(object sender, EventArgs e)
{
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append(@"<script language='javascript'>");
    sb.Append(@"var lbl = document.getElementById('lblDisplayDate');");
    sb.Append(@"lbl.style.color='red';");
    sb.Append(@"</script>");

    ScriptManager.RegisterStartupScript(btnPostback,this.GetType(), "JSCR", sb.ToString(),false);

}

Detail : Add JavaScript programmatically using RegisterStartupScript during an Asynchronous postback


In my case ScriptManager.RegisterStartupScript didn't work too.

Not work:

ScriptManager.RegisterStartupScript(Me, 
  Me.GetType(), 
  String.Format("Data{0}", Me.ID), 
  "<script>alert(111);</script>", 
  False)

Work:

ScriptManager.RegisterStartupScript(Me.Page, 
  Me.GetType(), 
  String.Format("Data{0}", Me.ID), 
  "<script>alert(111);</script>", 
  False)

Me in the example is my custom control inherited from System.Web.UI.WebControls.WebParts.WebPart