Search code examples
sharepointevent-handlingweb-parts

Web parts, dynamically created controls and eventhandlers


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?

For example, imagine some financial data:

Table 1:  Quarters in year

         |   Q1  |  Q2  |  Q3  |  Q4  |
Things 1 |   23  |  34  |  44  |  32  |
Things 2 |   24  |  76  |  67  |  98  |

On clicking on the value for Q2, Things 1 (34), this will lead to a second table being displayed instead of Table 1:

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  |

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.

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 after CreateChildControls, I only know that I need to change the table after CreateChildControls.

Thus, wherever I create Table 2 as a resault (since its after CreateChildControls), I cant get it to wire up events correctly.

Any thoughts?

Regards Moo

Edit: Solved it.

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.


Solution

  • At Alex's suggestion, here is the answer:

    The events need to be tied up prior to them being called, so you need to create the same control in CreateChildControls, allow the event to be called and then resetup everything afterward.

    To do this, first do CreateChildControls identically to the prior page, then check in OnPreRender if any eventhandler calls have been made, set any flags you need to and then call this.CreateChildControls manually with the new setup information so the new table is created and everything is wired up correctly.