I use LoadControl method in Load event quite extensively. However I haven’t observed any problems yet, I’m afraid of what MSDN documentation says:
When you load a control into a container control, the container
raises all of the added control’s events until it has caught up to the
current event. However, the added control does not catch up with
postback data processing. For an added control to participate in
postback data processing, including validation, the control must be
added in the Init event rather than in the Load event.
What does it actually mean?
Are there any other pitfalls when loading a control in the Load event?
That bit of MSDN documentation is (mostly) wrong. As you’ve discovered, postback data processing and validation work even if you dynamically add controls in the
Loadevent.Here are the stages of the ASP.NET page life cycle that are relevant to this question:
Initevent.Loadevent.The documentation is correct when it says that “the added control does not catch up with postback data processing”. But it overlooks the fact that there are two attempts to load posted form data, once before the
Loadevent and once after. Thus, if you dynamically add a control in theLoadevent, it will be populated with posted form data by the time the postback event (such assubmitButton_Click) occurs.As far as I can tell, here’s the main difference and potential pitfall:
Init, you can access its posted form data inLoad.Load, you have to wait until the postback event (or else access theHttpRequest.Formcollection directly).