I donβt think that creating controls in PageLoad is right now, first of all, the asp.net life cycle comes from initialization, Load ViewState Data; Load PostData; Loading objects, etc.
if you create controls on the_Load page, you will lose ViewState, events, etc.
Immediately done in PageInit, or if it is a control (OnInit).
The next tricky one is that in PageInit you donβt have a ViewState available, if you need to restore the number of objects needed to store some context / information in a hidden ant field, then get this information in PageInit, Create objects and voila!
Example:
Imagine you need to create 1.NN TextBoxes, you create a hidden html field (not with runat = server), for example. NumberOfTextBoxes.
When you execute the PageInit code: you retrieve the value, for example. numberOfTextBoxes = Request.Form ["NumberOfTextBoxes"], then you create TextBoxes.
Remember that the most important thing is to combine the number and order of existing controls stored in ViewState.
source share