Monday, January 26, 2015

ASP.Net Page life cycle

Page Framework Initialization - Page.Init

In this stage the ASP.Net creates the web page and generates all controls which are defined on the web page in the form of tags. If the page is not being request for the first time, ASP.Net deserialize the view state and apply it to all controls.


User Code Initialization - Page.Load

If the page is being requested for the first time or it is the part of postback the event Page.Load is always called. The required initialization of controls can be done in this stage. ASP.Net allows you to distinguish between the first time page is being loaded and it is being postback. This is just to boost the performance by loading the dynamic data on the very first load of page not on the postback. On the postback, ASP.Net restore the control’s properties from the view state. To determine the current state of the page you can use the IsPostBack property of the page like this




If (!IsPostBack)
{
//do something here.
}


Validation and Event handling

Validation:
Some validation controls are included in ASP.Net to validate other user input controls and error messages. These validation controls are self sufficient, means you don’t need to take care of the validation events. The validation occurs after the Page.Load event and before the any other event fired.
Event handling:
At this stage the web page is fully loaded and validated. Now ASP.Net fire all those events that have taken place since last postback. Events have been categorized in following two parts.
  1. Immediate response events: like button click, click on different links, image click, or image button click (that trigger the postback by calling the _doPostBack)
  2. Change Events: These events fired in changing of selections or text change in text box. These events called if the AutoPostBack property is set to TRUE for the control otherwise they fire the next time page is postback.


Automatic Data Binding - Page.PreRender

ASP.Net automatically perform updates and queries against your data source controls as a part of page life cycle. Essentially there are two type of data source operations.
  1. Before firing of Page.PreRender event: Operations like insert, update or delete performed after all controls events but before Page.PreRender event fires.
  2. After firing of Page.PreRender event: The data source controls perform their queries and insert the retrieved data to their binding controls.
The Page.PreRender event is suppose to be the last action in page life cycle before the page is rendered to HTML. During the pre-render, the page and controls are available so for, some actions can be performed here like storing some additional information to view state.


Cleanup - Page.Unload

At the end of the page life cycle, when web page has been rendered to HTML the Page.Unload event fired. The .Net framework garbage collector service runs to clean up the orphan objects to release the unnecessary occupied memory. When garbage collector has done with clean up, the Page.Dispose event fires. This is the end of the page life cycle.

Friday, January 23, 2015

How ASP.Net controls are connected to code behind class variables

- The Page class from the .Net library provide some basic functionality that allow web page to host other controls, and render itself to HTML.

- The code behind file is inherited from the Page class of .Net library to acquire the basic asp.net web page functionality.

- When you compile your code the ASP.NET automatically merge some extra code to your code behind class. This automatically generated code defines all control from the web page as the protected variables to your code behind class, so that you can access them in your code behind class.

- The ASP.NET compiler create one more extra class to represent the actual .aspx page. This class inherits from the your custom code behind class. ASP.NET add _aspx to the name of code behind class to name this extra class for example (theCodeBehindClass_aspx). This class contains the code needed to initialize the page and its controls and this is the class that ASP.NET instantiates when it receives the page request.
All the controls in the code behind class are protected because of the way of inheritance implemented in .Net library for Page class. The protected variable act like the private variables and the protected variables ensure that they can only be accessed from the derived page classes. So, this is the way how ASP.Net match the control variables to their respective control tags and attach event handlers at run time.