ASP.Net Page Life Cycle Events with Example


ASP.Net Page Life Cycle Events with Example

When an ASP.Net page runs, the page goes through a life cycle. These include initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering. In this article we will talk about application life cycle in detail with Example.

ASP.Net Web Page Life Cycle

In order to memorize whole page life cycle process of, keep store this word "SILVER" in your memory, which is defined as :
S = Start, I = Initialization, L = Load, V = Validate, E = Event Handlers, R = Render

General Page Life Cycle Stages

Some parts of the life cycle occur only when a page is processed as a postback. For postbacks, the page life cycle is the same during a partial-page postback as when you use an UpdatePanel control as it is during a full-page postback.
  • Page Request
  • Start
  • Initialization
  • Load
  • PostBack Event Handling
  • Rendering
  • Unload

ASP.Net Page Life Cycle Events

    • Check for the IsPostBack property to determine whether this is the first time the page is being processed.
    • Create or recreate dynamic controls.
    • Set master page dynamically.
    • Set the Theme property dynamically.
    • Read or set profile property values.
Note - If Request is postback
    • The values of the controls have not yet been restored from view state.
    • If you set control property at this stage, its value might be overwritten in the next event.
    • In the Init event of the individual controls occurs first, later the Init event of the Page takes place.
    • This event is used to initialize control properties.
    • Tracking of the ViewState is turned on in this event.
    • Any changes made to the ViewState in this event are persisted even after the next postback.
    • This event processes the postback data that is included with the request.
    • In this event the Page object calls the OnLoad method on the Page object itself, later the OnLoad method of the controls is called.
    • Thus Load event of the individual controls occurs after the Load event of the page.
    • This event is used to handle specific control events such as a Button control’s Click event or a TextBox control’s TextChanged event.
Note - In case of postback
    • If the page contains validator controls, the Page.IsValid property and the validation of the controls takes place before the firing of individual control events.
    • This event occurs after the event handling stage.
    • This event is used for tasks such as loading all other controls on the page.
    • In this event the PreRender event of the page is called first and later for the child control.
    • This method is used to make final changes to the controls on the page like assigning the DataSourceId and calling the DataBind method.
    • This event is raised after each control's PreRender property is completed.
    • This is raised after the control state and view state have been saved for the page and for all controls.
    • The page object calls this method on each control which is present on the page.
    • This method writes the control’s markup to send it to the browser.
    • This event is raised for each control and then for the Page object.
application life cycle,web application asp, page life cycle,page life cycle
List of events in ASP.Net Page Life Cycle Events
For better understanding I have created below example in

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">
<head runat="server">
    <title>Page Life Cycle Events</title>
    <form id="form1" runat="server">
        <asp:Button ID="btnEnter" runat="server" Text="Page Button"
            onclick="btnEnter_Click" />

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-Init <br />");

    protected override void OnInit(EventArgs e)
        Response.Write("" + (++index).ToString() + "  Init <br />");

    protected void Page_InitComplete(object sender, EventArgs e)
        Response.Write("" + (++index).ToString() + "  Init Completed <br />");

    protected void Page_Load(object sender, EventArgs e)
        ViewState["test"] = "Test";
        Response.Write("" + (++index).ToString() + "  Load <br />");

    protected override void OnPreRender(EventArgs e)
        Response.Write("" + (++index).ToString() + "  Pre Render <br />");

    //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 <br />");
        return base.SaveViewState();
    public void RaisePostBackEvent(string eventArgument)
        Response.Write("" + (++index).ToString() + "  Raise Post Back Event <br />");

    public void RaisePostDataChangedEvent()
        Response.Write("" + (++index).ToString() + "  Raise Post Data Change Event <br />");

    protected override void LoadViewState(object o)
        Response.Write("" + (++index).ToString() + "  Load View State <br />");

    private void Page_LoadComplete(object sender, System.EventArgs e)
        Response.Write("" + (++index).ToString() + "  Load Completed <br />");
    // On Button Click Event
    protected void btnEnter_Click(object sender, EventArgs e)
        Response.Write("" + (++index).ToString() + "  Button Click <br />");
    1. When a page request sent to the web server initially.
    2. On button's click event
application life cycle
asp net page lifecycle events
web application asp
asp net page life cycle with example
I hope you will enjoy the tutorial while learning agile process model and covered the agile approach, agile workflow, agile technologies and benefits of agile methodology. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome. 

Also If you like this article, don't forget to share this article with your friends and colleagues.
ASP.Net Page Life Cycle Events with Example Reviewed by Ravi Kumar on 5:54 PM Rating: 5

1 comment:

All Rights Reserved by Etechpulse © 2012 - 2017

Contact Form


Email *

Message *

Powered by Blogger.