Sunday, March 26, 2006

Dynamically create control in PreRender, not OK

Did something pretty silly today. I was trying to dynamically create some link buttons in the Page PreRender stage, because at that point DataBinding has already occurred, and the link buttons need to have the info from the DataBinding stage.

It turns out that you can't do that..read someone's post below:

Hi,

on postback the control would need to be loaded at Page_Load at the latest
for postback events to be raised. The page lifecycle will explain this you:

1. Instantiate
2. Initialize
3. TrackViewState
4. LoadViewState (postback)
5. Load postback data (postback, IPostBackDatahandler.LoadPostdata)
6. Load
7. Load postback data for dynamical controls added on Page_Load
8. Raise Changed Events (postback,
IPostBackDatahandler.RaisePostDataChanged)
9. Raise postback event (postback, IPostBackEventHandler.RaisePostBackEvent)
10.PreRender
11. SaveViewState
12. Render
13. Unload
14. Dispose

As you can see. postback events are raised just after Page_load, so controls
need to exist at that time ( at Load, because actual postback data loading
happens in phase 7)

If you can't change where the control is added, you need to manually inspect
from Form post (Request.Form) collection, which button was clicked and raise
the event.

0 Comments:

Post a Comment

<< Home