title: Adding to page control collection from inside a user control
body: <p>I have an asp.net usercontrol which represents a "popup" dialog.  Basically, it's a wrapper for the jQuery UI dialog which can be subclassed to easily make dialogs.</p>
  
  <p>As part of this control, I need to inject a div into the page the control is used on, either at the very top or very bottom of the form so that when the popup is instantiated, it's parent is changed to this div.  This allows "nested" popups without the child popup being trapped inside the parent popup.</p>
  
  <p>The trouble is, I can't find a safe way to inject this div into the page.  A usercontrol doesn't have a preinit event, so I can't do it there, and calling Page.Form.Controls.Add(...) in Init, Load or PreRender causes the standard exception "The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases."</p>
  
  <p>I thought I had found a solution by using...</p>
  
  <pre><code>ScriptManager.RegisterClientScriptBlock(Page, Me.GetType, UniqueID + "_Dialog_Div", containerDiv, False)
  </code></pre>
  
  <p>... which seemed to work well normally, but recently a coworker tried putting an UpdatePanel inside the dialog and now she's getting the error "The script tag registered for type 'ASP.controls_order_viewzips_ascx' and key 'ctl00$ContentBody$OViewZips_Dialog_Div' has invalid characters outside of the script tags: . Only properly formatted script tags can be registered."</p>
  
  <p>How are you supposed to add controls to the pages control collection from inside a user control?</p>