Server-side stuff
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.
Page_LoadComplete not automatically hooked up
Several articles online mentioned that AutoEventWireUp will wire up LoadComplete event. That does not seem to be the case, a quick test and you will find that this event is not fired in your page class automatically.No DataBind( ) call needed for GridView
I was coding gridview for the first time today, then I realized I didn't need to call Page.DataBind() for the data-binding to occur.Turns out that "databinding expressions are resolved automatically during the control's PreRender event and you are not required to call the DataBind method explicitly." - according to MSDN.
So what's the difference between Bind( ) and Eval( )?
Use Bind( ) if you need a two-way link, which essentially means if your control is updateable.
A good article on databinding can be found here:
http://msdn2.microsoft.com/en-us/library/ms178366(VS.80).aspx
