As per MSDN, we should use PreInit() event to Create or re-create dynamic controls.
And Init() event should be used to initialize control properties.
My question is, will there be some issue if we initialize the control properties in PreInit() instead of Init()? If No issue, then for what purpose should we use the OnInit() event?
Below is the MSDN page life cycle link which states this:
http://msdn.microsoft.com/en-us/library/ms178472.aspx
One reason is that if your custom control builds its children in the OnInit stage and if it needs some data (property) to build those controls, you cannot assign that property from the OnInit stage of the page because the OnInit get called in the order from lowest leaf controls up the page.
Therefore the only place (in the code-behind) you can assign a property to a control which relies on some supplied data to build children in it's OnInit is in the PreInit of the page.
Your Question 1:
will there be some issue if we initialize the control properties in PreInit()
instead of Init()
In PreInit() event, controls are still not initialized completely, so it is not recommended to use this event to set Control properties.
Question 2: for what purpose should we use the OnInit() event
If question 1 is clear, then you agree that you will initialize Control properties in OnInit() as also is said by MSDN.
Also, there is something important to understand about Init event and Control properties.
In Init you cannot access any controls properties after the last viewstate. Suppose you enter some text in a textbox control and click on a button control that makes a postback, now you want to view the value latest entered. In such case you will be able to see the textbox value which you’ve entered after InitComplete i.e. in PreLoad, since view state are loaded completely with PreLoad event onwards.
So, now you can say that if you set any control properties in Init event, there is possibility that it will be overridden when the next event InitComplete is raised & the tracking of view state changes is turned on
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With