I have two buttons, dynamically added to the page with OnClick event handler assigned to them. I want to place on page one of two different user controls at runtime when a corresponding button click occures. But here I meet the vicious circle: I can't add user control in OnLoad event handler of page, because I don't know which button have been pressed (event handling takes place after OnLoad). Also I can't add the user control after event handling (i.e. in OnPreRender), because in such case none of user control children event handlers would be fired (there would be no such child controls in moment where event handling occures - between OnLoad and OnPreRender). I've found 2 workarounds to this problem:
- I can parse Request.Form collection in OnLoad to determine event sender and command argument and create corresponding user control.
- I can add controls guided by previous page state in OnLoad, then if a button click have occured, remove inproper and add proper controls to the page in click handler.