title: Web parts, dynamically created controls and eventhandlers
body: <p>What is the best way to display, in a web part, dynamic tables where each cell can cause a postback to display a different set of data?</p>
  
  <p>For example, imagine some financial data:</p>
  
  <pre>
  Table 1:  Quarters in year
  
           |   Q1  |  Q2  |  Q3  |  Q4  |
  Things 1 |   23  |  34  |  44  |  32  |
  Things 2 |   24  |  76  |  67  |  98  |
  
  </pre>
  
  <p>On clicking on the value for Q2, Things 1 (34), this will lead to a second table being displayed instead of Table 1:</p>
  
  <pre>
  Table 2:  Weeks in Quarter
  
              |   W1  |  W2  |  W3  |  W4  |  W5  |  W6  |  W7  |
  SubThings 1 |   231 |  22  |  44  |  22  | 344  |  86  |  12  |
  SubThings 2 |   14  |  75  |  47  |  108 | 344  |  86  |  12  |
  
  </pre>
  
  <p>The problem with the approach I am taking at the moment is that I can create Table 1 in CreateChildControls, which leads to all the events being wired up fine for all the linkbuttons in the cells.</p>
  
  <p>However, because on the postback, I need to create Table 1 in CreateChildControls again, in order to have the eventhandlers correctly wired up, and as the events fire <em>after</em> CreateChildControls, I only know that I need to change the table <em>after</em> CreateChildControls.</p>
  
  <p>Thus, wherever I create Table 2 as a resault (since its after CreateChildControls), I cant get it to wire up events correctly.</p>
  
  <p>Any thoughts?</p>
  
  <p>Regards
  Moo</p>
  
  <p>Edit:  Solved it.</p>
  
  <p>What you need to do is check in OnPreRender any eventhandler calls, set any flags you need to and then call this.CreateChildControls manually so the new table is created and everything is wired up correctly.</p>