Beruflich Dokumente
Kultur Dokumente
1 of 6
http://www.codeproject.com/Articles/667308/ASP-NET-Page-Life-Cyc...
CPOL
4.82 (9 votes)
When a page request is sent to the Web server, the page is run through a series of events
during its creation and disposal. In this article, I will
Editorial Note
This articles was originally at wiki.asp.net but has now been given a new home on
CodeProject. Editing rights for this article has been set at Bronze or above, so please go in
and edit and update this article to keep it fresh and relevant.
When a page request is sent to the Web server, the page is run through a series of events
during its creation and disposal. In this article, I will discuss in detail the ASP.NET page
life cycle Events
(1) PreInit The entry point of the page life cycle is the
pre-initialization phase called PreInit. This is the only event where
programmatic access to master pages and themes is allowed. You can
dynamically set the values of master pages and themes in this event. You
can also dynamically create controls in this event.
EXAMPLE : Override the event as given below in your code-behind cs file
of your aspx page
using System;
using System.Collections.Generic;using System.Linq;using
System.Web;using System.Web.UI;using
System.Web.UI.WebControls; public partial class _Default :
System.Web.UI.Page{
protected void Page_PreInit(object sender,
EventArgs e)
{
// Use this event for the
following:
// Check the IsPostBack property to determine
whether this is the first time the page is being
// Create or re-create dynamic
processed.
controls.
// Set a master page dynamically.
//
Set the Theme property dynamically.
}
11/6/2014 9:38 PM
2 of 6
http://www.codeproject.com/Articles/667308/ASP-NET-Page-Life-Cyc...
-----------------------------------------------------------------------(2)Init This event fires after each control has been initialized, each
control's UniqueID is set and any skin settings have been applied. You
can use this event to change initialization values for controls. The
Init event is fired first for the most bottom control in the
hierarchy, and then fired up the hierarchy until it is fired for the
page itself.
EXAMPLE : Override the event as given below in your code-behind cs file
of your aspx page
11/6/2014 9:38 PM
3 of 6
http://www.codeproject.com/Articles/667308/ASP-NET-Page-Life-Cyc...
control state with the correct postback data. ASP.NET updates the
correct control by matching the control's unique ID with the name/value
pair in the NameValueCollection. This is one reason that ASP.NET
requires unique IDs for each control on any given page.
EXAMPLE : Override the event as given below in your code-behind cs file
of your aspx page
that by now, the page has been restored to its previous state in
case of postbacks. Code inside the page load event typically
checks for PostBack and then sets control properties
appropriately. This method is typically used for most code, since
this is the first place in the page lifecycle that all values are
restored. Most code checks the value of IsPostBack to avoid
unnecessarily resetting state. You may also wish to call Validate
and check the value of IsValid in this method. You can also create
dynamic controls in this method.
EXAMPLE : Override the event as given below in your code-behind cs file
of your aspx page
11/6/2014 9:38 PM
4 of 6
http://www.codeproject.com/Articles/667308/ASP-NET-Page-Life-Cyc...
11/6/2014 9:38 PM
5 of 6
http://www.codeproject.com/Articles/667308/ASP-NET-Page-Life-Cyc...
Note: Right click on the web page displayed at client's browser and
view the Page's Source. You will not find any aspx server control in the
code. Because all aspx controls are converted to their respective HTML
representation. Browser is capable of displaying HTML and client side
scripts.
EXAMPLE : Override the event as given below in your code-behind cs file
of your aspx page
License
This article, along with any associated source code and files, is licensed under The Code
Project Open License (CPOL)
11/6/2014 9:38 PM
6 of 6
http://www.codeproject.com/Articles/667308/ASP-NET-Page-Life-Cyc...
Share
About the Author
ASP.NET Community
United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks
spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How
To" questions. There's piles of fantastic community-created and MSFT-created content out
there, but if it's not found by a search engine and the right combination of keywords, it's
often lost.
The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved,
protected and updated by the community.
Group type: Collaborative Group
410 members
Select Language
Article Copyright 2013 by ASP.NET Community
Everything else Copyright CodeProject, 1999-2014
11/6/2014 9:38 PM