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:
- Page_Init
- LoadViewState
- LoadPostData
- Page_Load
- RaisePostDataChangedEvent
- RaisePostBackEvent
- Page_PreRender
- SaveViewState
- Page_Render
- Page_UnLoad
The Page Life Cycle Events Explained :
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:
- <%@ Page Language="C#" AutoEventWireup="true" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <script runat="server">
- protected void Button1_Click(object sender, System.EventArgs e)
- {
- BulletedList1.Style.Add("Padding", "10px 25px 10px 55px");
- BulletedList1.Style.Add("font-family", "Courier New");
- BulletedList1.Style.Add("font-size", "x-large");
- BulletedList1.Style.Add("font-style", "italic");
- BulletedList1.Style.Add("text-decoration", "underline");
- BulletedList1.Style.Add("border", "2px dotted darkred");
- BulletedList1.Style.Add("background-color", "pink");
- BulletedList1.Style.Add("color", "deeppink");
- }
- </script>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- <title>How to create and apply CSS style programmatically in asp.net control</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h2 style="color:MidnightBlue; font-style:italic;">
- How to create and apply CSS style
- <br /> programmatically in asp.net control
- </h2>
- <hr width="450" align="left" color="SkyBlue"/>
- <asp:BulletedList
- ID="BulletedList1"
- runat="server"
- Width="500"
- BackColor="Crimson"
- ForeColor="Snow"
- >
- <asp:ListItem>Crimson</asp:ListItem>
- <asp:ListItem>MidnightBlue</asp:ListItem>
- <asp:ListItem>CornFlower</asp:ListItem>
- <asp:ListItem>DarkSalmon</asp:ListItem>
- <asp:ListItem>IndianRed</asp:ListItem>
- <asp:ListItem>OliveDrab</asp:ListItem>
- </asp:BulletedList>
- <asp:Button
- ID="Button1"
- runat="server"
- OnClick="Button1_Click"
- Text="Create And Apply CSS Style In BulletedList"
- Height="45"
- Font-Bold="true"
- ForeColor="DarkBlue"
- />
- </div>
- </form>
- </body>
- </html>
0 comments:
Post a Comment