title: LinkButton postback but click event not fired
body: <p>Hi everyone<br><br>This is my first post on stackoverflow (which btw is by far my favourite site for finding answers). After finally admitting defeat, I'm hoping someone can help me with this problem...</p>
  
  <p>The question has already been asked several times, but none of the suggested solutions I found has helped. Apologies in advance for a lengthy post, but I want to avoid anyone wasting their time on suggesting things I tried already.</p>
  
  <p>The code below worked until recently, and its structure has not been touched since (although unrelated changes were made to the child page). In any event, it suddenly stopped working. Now even the most simplified button won’t fire.<br> <br> </p>
  
  <p><strong>Setup</strong></p>
  
  <ul>
  <li>VS 2008 C#, IIS 7 (no changes in setup since way before it stopped working) <br>  <br> </li>
  <li>nested masterpage (main + 1 child) <br>  <br> </li>
  <li>dynamically loaded ucl with datalist in child page<br>
  (i.e. MP => nested MP => child page => ucl => datalist => linkbutton) <br>  <br> </li>
  <li>linkbutton click event also resides in ucl</li>
  </ul>
  
  <p><br> 
  <strong>Problem</strong></p>
  
  <p>On LB click, the postback occurs ok, but the server-side click event never gets hit.
  <br> <br> </p>
  
  <p><strong>Code</strong></p>
  
  <p>page:</p>
  
  <pre><code>var ctrlX = base.LoadControl("~/somedir/someucl.ascx");
  
  =&gt; loads fine
  </code></pre>
  
  <p><br>  <br> 
  ascx file (datalist stripped of all but the button):</p>
  
  <pre><code>&lt;asp:DataList ID="dlX" RepeatLayout="Table" runat="server"&gt;
  &lt;HeaderTemplate&gt;&lt;/HeaderTemplate&gt;
  &lt;ItemTemplate&gt;
      &lt;tr&gt;
          &lt;td&gt;
              &lt;asp:LinkButton ID="btnX" OnClick="btnX_Click" CausesValidation="false" runat="server" /&gt;
          &lt;/td&gt;
      &lt;/tr&gt;               
  &lt;/ItemTemplate&gt;
  &lt;FooterTemplate&gt;&lt;/FooterTemplate&gt;
  </code></pre>
  
  <p></p>
  
  <p><br>  <br> 
  codebehind:</p>
  
  <pre><code>protected void btnX_Click(object sender, EventArgs e)
  {
  // do something  
  }
  </code></pre>
  
  <p><br> <br> 
  <strong>things tried</strong></p>
  
  <ul>
  <li>cleaning the solution</li>
  <li><p>digging out a working backup and checking for code changes <br> 
  (found none affecting the structure or user control)</p></li>
  <li><p>setting LinkButton CausesValidation true/false</p></li>
  <li>stripping datalist and LinkButton down to bare essentials</li>
  <li>adding AddressOf="btnX.click" to LinkButton</li>
  <li><p>wrapping UpdatePanel one at a time with varying settings around<br> usercontrol  <br> datalist  <br> linkbutton </p></li>
  <li><p>reattaching eventhandler in usercontrol init / load event:</p>
  
  <pre><code>IEnumerable&lt;Control&gt; controls = Utils.FlattenChildren(dlX);
  
  foreach (var button in controls.OfType(LinkButton)())
  {
      if (button.ID.Contains("btnX"))
          button.Click += new EventHandler(btnX_Click);
  }
  </code></pre>
  
  <p>Wherever I add above code, all buttons are found and the event is attached, but click event doesn't fire ((LinkButton) = LinkButton inside &lt;>; couldn't get it to display right, still struggling a bit with the editor)
  .</p></li>
  <li><p>adding PostBackUrl manually in page load event</p></li>
  <li>comparing the client ids between load/postback events
  => they always match</li>
  </ul>
  
  <p><br> <br> 
  That's it. Right now, I can't think of what else to try or check (maybe something in the breakpoint context menu on postback?). </p>
  
  <p>Because the ucl loads fine, and the postback is working ok, I suspect the problem is somewhere in the ucl, rather than the child page loading it. But maybe not.</p>
  
  <p>If at all possible, I want to avoid workarounds (I need the button command argument, not shown above, and am not keen on solutions such as jquery with hidden field or query string).</p>
  
  <p>Apart from anything, I would really like to understand what causes this.</p>
  
  <p>Obviously, I'm missing something... Thanks to anyone taking their time reading/helping with this!</p>
  
  <p><br><br>
  <strong>======== as requested additional codebehind =======</strong><br><br>
  I've simplified the parent page code to a minimum, dropping all method calls, and as per Eoins suggestion moved the ucl load in the page's init event. The ucl code remains as shown above. The DL and LB show up fine, and on click the postback is triggered, ucl page load event is hit, but as before, the server click event is not hit.</p>
  
  <pre><code>    protected override void OnInit(EventArgs e)
      {
          var ctrlX = base.LoadControl("~/someucl.ascx");
  
          if (ctrlX == null)
              return;
  
          DataList dlX = (DataList)ctrlX.FindControl("dlX");
  
          if (dlX == null)
              return;
  
          DummyCollection x = new DummyCollection();
  
          x.Add(null); // ok since the test LB does not draw on the datasource
          dlX.DataSource = x;
          dlX.DataBind();
          pnlX.Controls.Add(ctrlX);
  
          base.OnInit(e);
      }
  </code></pre>