Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

page Preinit, Init, load

Tags:

asp.net

I am having a doubt in page_init, page preinit, load. I need to know when we use this also where we need to call our objects in different stages of our life cycle.

Please let me know how they will process for each events raised

like image 247
balaweblog Avatar asked Apr 19 '26 22:04

balaweblog


2 Answers

Page events happen first before user control events. So the pages PageInit event fires, then the all user controls PageInit events fire. The pages PageLoad event fires, then all user controls PageLoad events fire. etc...

Sometimes developers will put initialization of private objects in their user controls PageLoad handler.

A common gotcha then occurs if the pages PageLoad handler calls a user control method which makes use of uninitialized private objects. Since the user controls PageLoad event hasn't fired yet, those objects are still "null" and an exception is throw (Object not set to instance of Object).

I then typically use the PageInit handler to initialize internal objects within a user control. That way they are not "null" when the public methods that use them are called.

This technique works if you don't call any user control methods from the pages PageInit handler. In my opinion, you shouldn't, though. That is not what handling PageInit in the pages code is for. Use the pages PageLoad handler for user control method calls.

like image 101
Chad Braun-Duin Avatar answered Apr 21 '26 12:04

Chad Braun-Duin


Its called the Page lifecycle because at different stages of the page request, different objects are populated with different information.

Here are some good links to read up on:

http://msdn.microsoft.com/en-us/library/ms178472.aspx

http://www.15seconds.com/issue/020102.htm

like image 30
Mark Avatar answered Apr 21 '26 14:04

Mark