This article provides a brief explanation of the what is asp net page life cycle events with example, asp net application lifecycle and after that its events. Often in asp.net interviews, you are asked to explain the Lifecycle of ASP.NET Page during its first request, as well as during Postback.
What is ASP NET Page Life Cycle
When ASP.Net page is called, that goes through a particular life cycle. This is done before the response is sent to the user. There are a series of steps which are followed for the processing of a life cycle of asp net page.
These include initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering.
Following are the different stages of an ASP.NET page:
- Page Request
- Start
- Initialization
- Load
- PostBack Event Handling
- Rendering
- Unload
ASP NET Application Life Cycle
When an ASP.Net application runs, there are a series of steps which are carried out. These series of steps make up the lifecycle of the application.
Let’s look at the various stages of a typical asp net application lifecycle:-
- Application Start
- Object Creation
- HttpApplication Creation
- Dispose
- Application End
ASP NET Page Life Cycle Events
Following are the page lifecycle events:
- PreInit()
- Init()
- InitComplete()
- PreLoad()
- Load()
- ControlEvents()
- LoadComplete()
- PreRender()
- PreRenderComplete()
- SaveStateComplete()
- RenderComplete()
- Unload()
ASP NET Page Lifecycle with Example
Now for better understanding, let’s look at the life cycle of asp net page with an example:-
using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class _Default : System.Web.UI.Page { int index = 0; #region PAGE LIFE CYCLE protected void Page_PreInit(object sender, EventArgs e) { Response.Write("" + (++index).ToString() + " Pre-Initlife cycle of asp net page
"); } protected override void OnInit(EventArgs e) { Response.Write("" + (++index).ToString() + " Init
"); } protected void Page_InitComplete(object sender, EventArgs e) { Response.Write("" + (++index).ToString() + " Init Completed
"); } protected void Page_Load(object sender, EventArgs e) { ViewState["test"] = "Test"; Response.Write("" + (++index).ToString() + " Load
"); } protected override void OnPreRender(EventArgs e) { Response.Write("" + (++index).ToString() + " Pre Render
"); } //protected override void Render(HtmlTextWriter writer) //{ // //Response.Write("" + (++i).ToString() + " Render"); //} protected override void OnUnload(EventArgs e) { // Response.Write("" + (++i).ToString() + " Unload"); } public override void Dispose() { // Response.Write("" + (++i).ToString() + " Dispose"); } protected override object SaveViewState() { Response.Write("" + (++index).ToString() + " Save View State
"); return base.SaveViewState(); } public void RaisePostBackEvent(string eventArgument) { Response.Write("" + (++index).ToString() + " Raise Post Back Event
"); } public void RaisePostDataChangedEvent() { Response.Write("" + (++index).ToString() + " Raise Post Data Change Event
"); } protected override void LoadViewState(object o) { Response.Write("" + (++index).ToString() + " Load View State
"); base.LoadViewState(o); } private void Page_LoadComplete(object sender, System.EventArgs e) { Response.Write("" + (++index).ToString() + " Load Completed
"); } // On Button Click Event protected void btnEnter_Click(object sender, EventArgs e) { Response.Write("" + (++index).ToString() + " Button Click
"); } #endregion }
Output
- When a page request sent to the web server initially.
- Similarly, we will check On button’s click event
Conclusion
I hope you liked this article to understand asp net page life cycle, asp net application lifecycle, and its events. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.
The post ASP NET Page Life Cycle Events with Example appeared first on DotNetTec.
This post first appeared on Asp Dot Net Tricks And Tips, Dot Net Coding Tips, Google Maps API Developer, please read the originial post: here