title: How to force a full postback on a button created in code behind and displayed in an UpdatePanel
body: <p>I am using an asp.net UpdatePanel that contains Button 1. On Button1 click, a string is created in code behind then sent back to the updatePanel. That string contains Button2 that should fire a postback to execute Button2_Click function. Unfortunately I haven't been able to execute a full postback on button 2 yet.</p>
  
  <p>If I add Button2 as a PostBackTrigger control in the UpdatePanel on my aspx page I get an error that the control (Button2) is not found. This makes sense cause Buton2 is created by clicking Button1 in code behind and is not available on page load.</p>
  
  <p>I'm trying to RegisterPostBackControl(Button2) on page load but again I get an error that Button2 doesn't exist in the current context. </p>
  
  <p>Any help would be highly appreciated.</p>
  
  <p>ASPX page</p>
  
  <pre><code>&lt;asp:ScriptManager ID="ScriptManager" runat="server" /&gt;
  &lt;asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server"&gt;
  &lt;ContentTemplate&gt;
     &lt;fieldset&gt;
  
     &lt;div runat="server" id="DIV1"&gt;Testing...&lt;br /&gt;&lt;/div&gt;
     &lt;asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" clientidmode="static"/&gt; 
  
     &lt;div runat="server" id="DIV2"&gt;&lt;/div&gt;
     &lt;asp:Label ID="DIV2_LABEL" runat="server"&gt;&lt;/asp:Label&gt;
     &lt;/fieldset&gt;
  &lt;/ContentTemplate&gt;
  </code></pre>
  
  <p></p>
  
  <p>CODE BEHIND</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
      {
          ScriptManager.GetCurrent(Page).RegisterPostBackControl(Button2);
      }
  
  protected void Button1_Click(object sender, EventArgs e)
      {
          string DIV1_Html;
          DIV1_Html= DIV1.InnerHtml;
  
          DIV2.InnerHtml = DIV1_Html;
          DIV2.InnerHtml += "&lt;input id='Button2' type='button' value='button' runat='server' OnClick='Button2_Click' /&gt;";
  
      }
  
   protected void Button2_Click(object sender, EventArgs e)
      {
  
          DIV2_LABEL.Text = "Testing button2";
      }
  </code></pre>
  
  <p>EDIT</p>
  
  <p>Based on the advice below, I've added the button as follows:</p>
  
  <p>Instead of:</p>
  
  <pre><code> DIV2.InnerHtml += "&lt;input id='Button2' type='button' value='button' runat='server' OnClick='Button2_Click' /&gt;";
  </code></pre>
  
  <p>I've amended the code as follows:</p>
  
  <pre><code> Button Button2 = new Button();
   Button2.Text = "Add";
   Button2.Click += new EventHandler(Button2_Click);
   PostBackTrigger pTrigger = new System.Web.UI.PostBackTrigger() { ControlID = Button2.ID };
   UpdatePanel1.Triggers.Add(pTrigger);
   DIV2.Controls.Add(Button2);
  </code></pre>
  
  <p>BUT I'm still missing something because Button2_Click function is not fired when I click on the new button with NO error. ?????</p>