I am pretty sure back in the days of ASP.NET 1.0/1.1, controls created during runtime needs to be added before Page_Load
event of the Page Lifecycle (i.e. inside Page_Init
).
Here's one article by Microsoft on it (for .NET 1.0/1.1):
HOW TO: Dynamically Create Controls in ASP.NET:
Note When you create dynamic controls on a Web Form, you must create the controls and add them to the controls collection in either the Page_Init event handler or the Page_Load event handler. Otherwise, the controls may not behave as expected.
However, in a few posts here, it seems like the above is not the case anymore. Controls added within Page_Load
seems to be working for everyone else. Some of the posts include:
http://stackoverflow.com/questions/886664/creating-dynamic-control-in-asp-net
http://stackoverflow.com/questions/1708016/viewstate-utter-confusion
I've tried it myself and indeed it worked though I've not done enough test to fish out any unexpected behavior.
So is Page_Load
a safe stage to add dynamic controls? Or is it only for .NET 2.0 and above?