if you have attended any interviews for web application developer post you might have come across this question for sure. From an interviewer's perspective, he is just checking whether you have any idea on page life cycle events other than OnLoad function and from your perspective this is the best chance to impress him. So what all you should know?
MSDN has a comprehensive article on this here
I would like to present it simple here with just a gist of all relevant events.
It all starts with
Page Init- This event has 3 sub events attached to it
PreInt, Init and InitComplete
PreInit event we use to dynamically change the theme and master page. Also we can use this event for create or recreate dynamic controls, read or set control property values etc. The main thing to keep in mind on using this is that ViewState values might not be completely set by this time, so if the page is in a post back the properties you set may be over written when the view state is set in the next event.
Init can be used to read or Initialize the control properties. The Init event for individual controls is called before that of page.
InitComplete is fired after the view state tracking is turned on. In this event you can change the view state values and no fear of losing data as view state tracking is on.
The next event is Load event which contains two sub events, PreLoad and Load.
PreLoad is raised after page loads view state and all the post back data which is included with request instance.
In load event, page object calls OnLoad function on the page object and then OnLoad methods for individual controls are called. This is done recursively for each child controls until every control is loaded to the page.
So this is what happens internally and we can use OnLoad event for creating database connections.
Control events come next which we use to handle individual events of controls like button click, DropDownList SelectedIndexChanged etc.
LoadComplete is fired at the end of control event handling stage. This can be used to wire events which require all the controls in the page to be loaded.
The next events/methods called are specific to rendering which includes PreRender, PreRenderComplete, SaveStateComplete and render.
In PreRender the page calls EnsureChildControls method for each control and for the page itself. This event ensures all the controls in the page including child controls and composite controls are created and ready to be rendered.
This event can be used to make changes in the pages before the rendering starts.
PreRender complete is raised after all the data bound control whose DatasourceID is set calls it’s DataBind method.
SaveStateComplete is raised after all the view state data and control state data have been saved and loaded. The state for next postback is saved now and further changes won’t be retrieved in next postback.
Render is a method which is there for each control which writes out the markup for each control to the browser. Also if you have created custom controls you might have overridded this method for writing rendering logic for your control.
Unload is an event which can be used for cleanup activities like closing DB connections, external file handles etc. It can also be used for logging purposes.
This is a brief of the page life cycle events and this article is only meant for providing an overview.
MSDN has a comprehensive article on this here
I would like to present it simple here with just a gist of all relevant events.
It all starts with
Page Init- This event has 3 sub events attached to it
PreInt, Init and InitComplete
PreInit event we use to dynamically change the theme and master page. Also we can use this event for create or recreate dynamic controls, read or set control property values etc. The main thing to keep in mind on using this is that ViewState values might not be completely set by this time, so if the page is in a post back the properties you set may be over written when the view state is set in the next event.
Init can be used to read or Initialize the control properties. The Init event for individual controls is called before that of page.
InitComplete is fired after the view state tracking is turned on. In this event you can change the view state values and no fear of losing data as view state tracking is on.
The next event is Load event which contains two sub events, PreLoad and Load.
PreLoad is raised after page loads view state and all the post back data which is included with request instance.
In load event, page object calls OnLoad function on the page object and then OnLoad methods for individual controls are called. This is done recursively for each child controls until every control is loaded to the page.
So this is what happens internally and we can use OnLoad event for creating database connections.
Control events come next which we use to handle individual events of controls like button click, DropDownList SelectedIndexChanged etc.
LoadComplete is fired at the end of control event handling stage. This can be used to wire events which require all the controls in the page to be loaded.
The next events/methods called are specific to rendering which includes PreRender, PreRenderComplete, SaveStateComplete and render.
In PreRender the page calls EnsureChildControls method for each control and for the page itself. This event ensures all the controls in the page including child controls and composite controls are created and ready to be rendered.
This event can be used to make changes in the pages before the rendering starts.
PreRender complete is raised after all the data bound control whose DatasourceID is set calls it’s DataBind method.
SaveStateComplete is raised after all the view state data and control state data have been saved and loaded. The state for next postback is saved now and further changes won’t be retrieved in next postback.
Render is a method which is there for each control which writes out the markup for each control to the browser. Also if you have created custom controls you might have overridded this method for writing rendering logic for your control.
Unload is an event which can be used for cleanup activities like closing DB connections, external file handles etc. It can also be used for logging purposes.
This is a brief of the page life cycle events and this article is only meant for providing an overview.
No comments:
Post a Comment