title: JavaScript code inside UpdatePanel
body: <p>Ok: I've got an UpdatePanel on an aspx page that contains a single Placeholder.</p>
  
  <p>Inside this placeholder I'm appending one of a selection of usercontrols depending on certain external conditions (this is a configuration page).</p>
  
  <p>In each of these usercontrols there is a bindUcEvents() javascript function that binds the various jQuery and javascript events to buttons and validators inside the usercontrol.</p>
  
  <p>The issue I'm having is that the usercontrol's javascript is not being recognised. Normally, javascript inside an updatepanel is executed when the updatepanel posts back, however none of this code can be found by the page (I've tried running the function manually via firebug's console, but it tells me it cannot find the function).</p>
  
  <p>Does anyone have any suggestions?</p>
  
  <p>Cheers, Ed.</p>
  
  <p>EDIT:</p>
  
  <p>cut down (but functional) example:</p>
  
  <p>Markup:</p>
  
  <pre><code>&lt;script src="/js/jquery-1.3.2.min.js"&gt;&lt;/script&gt;
    &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
  
      &lt;asp:ScriptManager ID="Script" runat="server" /&gt;
  
      &lt;asp:Button ID="Postback" runat="server" Text="Populate" OnClick="PopulatePlaceholder" /&gt;
  
      &lt;asp:UpdatePanel ID="UpdateMe" runat="server"&gt;
      &lt;Triggers&gt;
          &lt;asp:AsyncPostBackTrigger ControlID="Postback" EventName="Click" /&gt;
      &lt;/Triggers&gt;
          &lt;ContentTemplate&gt;
              &lt;asp:Literal ID="Code" runat="server" /&gt;
              &lt;asp:PlaceHolder ID="PlaceMe" runat="server" /&gt;
          &lt;/ContentTemplate&gt;
      &lt;/asp:UpdatePanel&gt;
      &lt;/div&gt;
   &lt;/form&gt;
  </code></pre>
  
  <p>C#:</p>
  
  <pre><code>protected void PopulatePlaceholder(object sender, EventArgs e)
  {
      Button button = new Button();
      button.ID = "Push";
      button.Text = "push";
      button.OnClientClick = "javascript:return false;";
      Code.Text = "&lt;script type=\"text/javascript\"&gt; function bindEvents() { $('#" + button.ClientID + "').click(function() { alert('hello'); }); }  bindEvents(); &lt;/script&gt;";
      PlaceMe.Controls.Add(button);
  }
  </code></pre>
  
  <p>You'll see that the button does not poput the alert message, even though the code is present on the page.</p>
  
  <p>EDIT2:</p>
  
  <p>Ok, just to make it clear, the production code is significantly more complex than just a single function bound onto a literal, and contains a large number of</p>
  
  <pre><code>&lt;%= Control.ClientID %&gt;
  </code></pre>
  
  <p>bits of code that'll be very difficult to factor off into non-specific functions, and pointless as they're each only used in one place (we're talking very specific validation and the odd popout trigger + some logic).</p>