Thursday 26 July 2012

ASP.Net Application Life Cycle | Page Life Cycle Events | Example - Elearnsite Tutorial



ASP.Net Application Life Cycle:
The application life cycle has the following stages:
·         User makes a request for accessing application resource, a page. Browser sends this request to the web server.
·         A unified pipeline receives the first request and the following events take place:
o    An object of the ApplicationManager class is created.
o    An object of the HostingEnvironment class is created to provide information regarding the resources.
o    Top level items in the application are compiled.
·         Response objects are created . the application objects: HttpContext, HttpRequest and HttpResponse are created and initialized.
·         An instance of the HttpApplication object is created and assigned to the request. The request is processed by the HttpApplication class. Different events are raised by this class for processing the request.

The ASP.NET Page Life Cycle:

Understanding the Page Life Cycle in ASP.NET is essential knowledge for developing ASP.NET apps, without a firm understanding of the Page Life Cycle developing apps will be an uphill battle.
When a web page is sent to the Web Server for processing, it goes through a sequence of steps before it finally gets displayed in the Web Browser. This article discusses these series of steps and events that occur in a page life cycle in ASP.NET.

From The Web Browser to IIS

When a POST request is initiated from the client side, the Web Server traps the request and it is usually routed to an .aspx web page. The request is actually routed to the HTTP Pipeline, a chain of managed objects.
After the HTTP Page handler class is identified, the ProcessRequest () method is called which eventually fires the different page events in the life cycle of a web page. The sequence of events that takes place in the life cycle of a web page in ASP.NET is:
  1. Page_Init
  2. LoadViewState
  3. LoadPostData
  4. Page_Load
  5. RaisePostDataChangedEvent
  6. RaisePostBackEvent
  7. Page_PreRender
  8. SaveViewState
  9. Page_Render
  10. Page_UnLoad
All these events are associated with their respective handlers and you can even override them to customize their default behaviour. The following section discusses each of these events in detail.

The Page Life Cycle Events Explained :

Once the request for the web page arrives at the web server, the ASP.NET runtime determines whether the page needs to be parsed or whether a cached version of the page needs to be rendered to the requestor. Then the Request and the Response objects for the page are set and the page life cycle starts.
The Page_Init event is the first event to be triggered in the page life cycle. It is responsible for the initialization activities that are essential to create a page instance. In this phase of the page life cycle, all the server controls of the web page are initialized to their default values. However, it should be noted that the View State for a page is not available at this stage of the page life cycle and a server control of the page cannot access other server controls of the page at this phase.
You can use the Page_Init event to create or re-create the controls that need to be created or re-created dynamically. The following example illustrates how you can override the OnInit() method.
protected override void OnInit(EventArgs e)
 {
if (Page != null)
{
Page.Trace.Write ("The OnInit method has been called");
base.OnInit(e);
Page.RegisterRequiresPostBack(this);
}
}

ASP.Net Page Life Cycle Events:

When the Page is requested for the first time


The Life Cycle of a page when requested for the first time:

Initializing: During this phase, the server creates an instance of the server control

Loading: During this phase, the instance of the control is loaded onto the page object in which it is defined.

PreRendering: During this phase, the control is updated with the changes made to it. This prepares the control for rendering.

Saving: During this phase, the state information of the control is saved. For example, if a value is set for the control during the Load event, it is embedded in the HTML tag that will be returned to the browser.

Rendering: During this phase, the server creates the corresponding HTML tag for the control.

Disposing: During this phase, all cleanup tasks, such as closing files and database connections opened by the control are performed.

Unloading: During this phase, all cleanup tasks, such as destroying the instances of server control are performed. This is the final event in the life cycle of a server control

Life cycle when the page processed during a postback event

The processing sequence in which a page is processed during a postback event is:

Initializing: During this phase, the server creates an instance of the server control

Loading view state: During this phase, the view state of the control posted by the client is reloaded into the new instance of the control.

Loading: During this phase, the instance of the control is loaded onto the page object in which it is defined.

Loading the postback data: During this phase, the server searches any data corresponding to the control that is loaded in the data posted by the client.

PreRendering: During this phase, the control is updated with the changes made to it. This prepares the control for rendering.

Saving state: During this phase, the change in the state of control between the current request and the previous request of the page is saved. For each change, the corresponding event is raised. For example, if the text of a textbox is changed, the new text is saved and a text_change event is raised.

Rendering: During this phase, the server creates the corresponding HTML tag 
for the control.

Disposing: During this phase, all cleanup tasks, such as closing files and database connections opened by the control are performed.

Unloading: During this phase, all cleanup tasks, such as destroying the instances of server control are performed. This is the final event in the life cycle of a server control

The events associated with the relevant page cycle phases are:
  • Page Initialization: Page_Init
  • View State Loading:LoadViewState
  • Postback data processing: LoadPostData
  • Page Loading: Page_Load
  • PostBack Change Notification: RaisePostDataChangedEvent
  • PostBack Event Handling: RaisePostBackEvent
  • Page Pre Rendering Phase: Page_PreRender
  • View State Saving: SaveViewState
  • Page Rendering: Page_Render
  • Page Unloading: Page_UnLoad

Asp.net basic examples:

  1. <%@ Page Language="C#" AutoEventWireup="true" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <script runat="server">  
  5.     protected void Button1_Click(object sender, System.EventArgs e)  
  6.     {  
  7.         BulletedList1.Style.Add("Padding", "10px 25px 10px 55px");  
  8.         BulletedList1.Style.Add("font-family", "Courier New");  
  9.         BulletedList1.Style.Add("font-size", "x-large");  
  10.         BulletedList1.Style.Add("font-style", "italic");  
  11.         BulletedList1.Style.Add("text-decoration", "underline");  
  12.         BulletedList1.Style.Add("border", "2px dotted darkred");  
  13.         BulletedList1.Style.Add("background-color", "pink");  
  14.         BulletedList1.Style.Add("color", "deeppink");  
  15.     }  
  16. </script>  
  17.   
  18. <html xmlns="http://www.w3.org/1999/xhtml">  
  19. <head id="Head1" runat="server">  
  20.     <title>How to create and apply CSS style programmatically in asp.net control</title>  
  21. </head>  
  22. <body>  
  23.     <form id="form1" runat="server">  
  24.     <div>  
  25.         <h2 style="color:MidnightBlue; font-style:italic;">  
  26.             How to create and apply CSS style  
  27.             <br /> programmatically in asp.net control  
  28.         </h2>  
  29.         <hr width="450" align="left" color="SkyBlue"/>  
  30.         <asp:BulletedList  
  31.              ID="BulletedList1"  
  32.              runat="server"  
  33.              Width="500"  
  34.              BackColor="Crimson"  
  35.              ForeColor="Snow"  
  36.              >  
  37.              <asp:ListItem>Crimson</asp:ListItem>  
  38.              <asp:ListItem>MidnightBlue</asp:ListItem>  
  39.              <asp:ListItem>CornFlower</asp:ListItem>  
  40.              <asp:ListItem>DarkSalmon</asp:ListItem>  
  41.              <asp:ListItem>IndianRed</asp:ListItem>  
  42.              <asp:ListItem>OliveDrab</asp:ListItem>  
  43.         </asp:BulletedList>  
  44.         <asp:Button   
  45.             ID="Button1"  
  46.             runat="server"  
  47.             OnClick="Button1_Click"  
  48.             Text="Create And Apply CSS Style In BulletedList"  
  49.             Height="45"  
  50.             Font-Bold="true"  
  51.             ForeColor="DarkBlue"  
  52.             />  
  53.     </div>  
  54.     </form>  
  55. </body>  
  56. </html>  

 



0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More