title: ScriptManager.RegisterClientScriptBlock registering scripts twice
body: <p>On the OnPreRender of my custom server control, I am registering an alert to popup everytime the page is loaded after a partial postback (add_pageLoaded(function() { alert('Hi')})). I only want this to be called once, but its gets called as many times as there has been partial postbacks. For example, in the code below, if you open the page and click "Click me" you get one alert, click again and you will get two alerts, three --> three alerts and so forth. My understanding was that ScriptManager.RegisterClientScriptBlock, using the scriptKey parameter was suppose to check if the script has already been registered, and if so, dont register it again.
    I cannot register this script on !IsPostBack because my custom control will be inserted during Postbacks into pages, so using !IsPostBack will not include my script.</p>
  
  <p>Here is a simple example of this problem:</p>
  
  <p>ASPX:</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"                                          Inherits="RegisterClientScriptExperimentation._Default" %&gt;
  &lt;%@ Register Assembly="RegisterClientScriptExperimentation"         Namespace="RegisterClientScriptExperimentation" TagPrefix="cc" %&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml" &gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;    
          &lt;asp:ScriptManager ID="ScriptManager1" runat="server"/&gt;       
          &lt;asp:UpdatePanel runat="server"&gt;
              &lt;ContentTemplate&gt;
                  &lt;asp:Panel ID="panel" runat="server"/&gt;                    
              &lt;/ContentTemplate&gt;
          &lt;/asp:UpdatePanel&gt;        
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Code behind:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Text;
  
  namespace RegisterClientScriptExperimentation
  {
      public partial class _Default : System.Web.UI.Page
      {
          protected override void OnInit(EventArgs e)
          {
              base.OnInit(e);
              panel.Controls.Add(new CustomControl());
          }
      }
  
      public class CustomControl : CompositeControl
      {
          private Button button;
          public CustomControl()
          {
              button = new Button();
              button.Text = "Click me";
          }
          protected override void CreateChildControls()
          {
              Controls.Clear();
              Controls.Add(button);
          }
          protected override void OnPreRender(EventArgs e)
          {
              base.OnPreRender(e);
  
              StringBuilder _text = new StringBuilder();
              _text.Append("var prm = Sys.WebForms.PageRequestManager.getInstance();");
              _text.Append("prm.add_pageLoaded(function() { alert('Page Loaded'); });");
  
              if (null != System.Web.UI.ScriptManager.GetCurrent(Page) &amp;&amp; System.Web.UI.ScriptManager.GetCurrent(Page).IsInAsyncPostBack)
                  System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "customControlScript", _text.ToString(), true);
              else
                  Page.ClientScript.RegisterStartupScript(this.GetType(), "customControlScript", _text.ToString(), true);
          }
      }
  }
  </code></pre>