title: Accessing dynamically added controls on postback
body: <p>I'm trying to get the dynamic <code>Control</code> that caused a postback.  All <code>Controls</code> are added dynamically to a <code>Panel</code>.  </p>
  
  <pre><code>&lt;asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent"&gt;
      &lt;asp:Panel runat="server" ID="panel"&gt;&lt;/asp:Panel&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>I have read that the two following methods should find the postback <code>Control</code> (at this point I am not concerned with <code>Buttons</code> causing a postback, but will be).</p>
  
  <pre><code>private Control GetPostBackControl()
              {
              Control toReturn = null;
              string controlName = page.Request.Params.Get("__EVENTTARGET");
              if (!String.IsNullOrEmpty(controlName))
              {
                  toReturn = RecurseControls(this.Page, controlName);
              }
             return toReturn;
      }
  
   private Control RecurseControls(Control root, String toFind)
          {
              String s = root.UniqueID;
              if (root.UniqueID == toFind)
              {
                  return root;
              }
              foreach (Control c in root.Controls)
              {
                  Control t = RecurseControls(c, toFind);
                  if (t != null)
                  {
                      return t;
                  }
              }
              return null;
          }
  </code></pre>
  
  <p>When stepping through the debugger, the recursion finds the panel, but there are no child <code>Controls</code> within it. I'm assuming that my understanding of how these methods work was initially off, and they cannot find dynamically added <code>Controls</code> before they are recreated for this <code>Post</code>. If this is the case, is there another way to find the dynamic <code>Control</code> that triggered the postback?  If not, is there a way to cause the page to re-post within a <code>Control's</code> <code>EventHandler</code>?</p>
  
  <p>Please note that I am using a master page.  Whether I use the recursive method as I have posted it, or like <code>RecurseControls(this.Master, toFind);</code> I still don't see the <code>Panel's</code> child <code>Controls</code>.  <code>GetPostBackControl()</code> is also called within <code>Page_Init()</code>.</p>