title: Add additional content into a known content placeholder from a user control
body: <p>I have a <code>UserControl</code> that has some javascript I'd like to inject into a known <code>ContentPlaceHolder</code>.</p>
  
  <p>I was hoping to do something like the following except when I append to add the control to found control I get an exception which says I cannot modify the control collection in the Init, Load or PreRender events:</p>
  
  <p>"UserControl.ascx"
  <pre><code>
   &lt;%@ Control Language="C#" %>
   &lt;asp:Checkbox runat="server" id="checkBox"/>
   &lt;app:JavascriptInjector runat="server" InjectInto="ScriptPlaceHolder">
        $(function(){ $('#&lt;%= checkBox.ClientID %>').click(function() { ... });
   &lt;/script>&lt;/app:JavascriptInjector>
  </pre></code></p>
  
  <p>"JavascriptInjector.cs"</p>
  
  <pre><code>using System;
  using System.Diagnostics;
  using System.Linq;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  public class JavascriptInjector : PlaceHolder
  {
      public string InjectInto
      {
          get { return this.ViewState["InjectInto"] as string; }
          set { this.ViewState["InjectInto"] = value; }
      }
  
      protected override void OnInit(EventArgs e)
      {
          base.OnInit(e);
  
          this.PreRender += this.__PreRender;
      }
  
      private void __PreRender(object sender, EventArgs e)
      {
          if (string.IsNullOrEmpty(this.InjectInto))
          {
              goto performRegularlly;
          }
  
          var injectInto = this.FindControlRecursively(this.Page);
  
          if (injectInto == null)
          {
              goto performRegularlly;
          }
  
          injectInto.Controls.Add(this);
  
          return;
  
          performRegularlly:
  
          Debug.WriteLine("performing regularlly");
      }
  
      private Control FindControlRecursively(Control current)
      {
          var found = current.FindControl(this.InjectInto);
  
          if (found != null)
          {
              return found;
          }
  
          foreach (var child in current.Controls.Cast&lt;Control&gt;())
          {
              return this.FindControlRecursively(child);
          }
  
          return null;
      }
  }
  </code></pre>