Thursday, 26 January 2023

ASP.NET Page Life Cycle Events

ASP.NET Page Life Cycle Events

ASP.NET Page Life Cycle Events at every stage of the page life cycle, the page raises some events, which could be coded. An event handler is basically a function or subroutine, bound to the event, using declarative attributes such as Onclick or handle.

Following are the page life cycle events: -

Ø  Init - Init event initializes the control property and the control tree is built. This event can be handled by overloading the OnInit method or creating a Page_Init handler.

Ø  InitComplete - InitComplete event allows tracking of view state. All the controls turn on view-state tracking.

Ø  LoadViewState - LoadViewState event allows loading view state information into the controls.


Ø  LoadPostData - During this phase, the contents of all the input fields are defined with the <form> tag are processed.


Ø  PreLoad - PreLoad occurs before the post back data is loaded in the controls. This event can be handled by overloading the OnPreLoad method or creating a Page_PreLoad handler.


Ø  Load - The Load event is raised for the page first and then recursively for all child controls. The controls in the control tree are created. This event can be handled by overloading the OnLoad method or creating a Page_Load handler.


Ø  LoadComplete - The loading process is completed, control event handlers are run, and page validation takes place. This event can be handled by overloading the OnLoadComplete method or creating a Page_LoadComplete handler.


Ø  PreRender - The PreRender event occurs just before the output is rendered. By handling this event, pages and controls can perform any updates before the output is rendered.


Ø  PreRenderComplete - As the PreRender event is recursively fired for all child controls, this event ensures the completion of the pre-rendering phase.


Ø  SaveStateComplete - State of control on the page is saved. Personalization, control state and view state information is saved. The HTML markup is generated. This stage can be handled by overriding the Render method or creating a Page_Render handler.


Ø  UnLoad - The UnLoad phase is the last phase of the page life cycle. It raises the UnLoad event for all controls recursively and lastly for the page itself. Final cleanup is done and all resources and references, such as database connections, are freed. This event can be handled by modifying the OnUnLoad method or creating a Page_UnLoad handler.


Please observe the code comments and output. It will help you to clearly understand the concepts: -

public partial class PageLiftCycle: System.Web.UI.Page

    protected void Page_PreInit(object sender, EventArgs e)

 { 

        //Work and It will assign the values to label

        lblName.Text = lblName.Text + "<br/>" + "PreInit"; 

    } 

    protected void Page_Init(object sender, EventArgs e) { 

        //Work and It will assign the values to label. 

        lblName.Text = lblName.Text + "<br/>" + "Init"; 

    } 

    protected void Page_InitComplete(object sender, EventArgs e) { 

        //Work and It will assign the values to label. 

        lblName.Text = lblName.Text + "<br/>" + "InitComplete"; 

    } 

    protected override void OnPreLoad(EventArgs e) { 

        //Work and It will assign the values to label. 

        //If the page is post back, then label control values will be loaded from view state. 

        //E.g: If you string str = lblName.Text, then str will contain viewstate values. 

        lblName.Text = lblName.Text + "<br/>" + "PreLoad"; 

    } 

    protected void Page_Load(object sender, EventArgs e) 

   { 

        //Work and It will assign the values to label. 

        lblName.Text = lblName.Text + "<br/>" + "Load"; 

    } 

    protected void btnSubmit_Click(object sender, EventArgs e)

   { 

        //Work and It will assign the values to label. 

        lblName.Text = lblName.Text + "<br/>" + "btnSubmit_Click"; 

    } 

    protected void Page_LoadComplete(object sender, EventArgs e) 

  { 

        //Work and It will assign the values to label. 

        lblName.Text = lblName.Text + "<br/>" + "LoadComplete"; 

    } 

    protected override void OnPreRender(EventArgs e) 

   { 

        //Work and It will assign the values to label. 

        lblName.Text = lblName.Text + "<br/>" + "PreRender"; 

    } 

    protected override void OnSaveStateComplete(EventArgs e) 

   { 

        //Work and It will assign the values to label. 

        //But "SaveStateComplete" values will not be available during post back. i.e. View state. 

        lblName.Text = lblName.Text + "<br/>" + "SaveStateComplete"; 

    } 

    protected void Page_UnLoad(object sender, EventArgs e) 

   { 

        //Work and it will not affect label contrl, view stae and post back data. 

        lblName.Text = lblName.Text + "<br/>" + "UnLoad"; 

    } 

}   




0 comments

Post a Comment