0% found this document useful (0 votes)
4 views16 pages

Lect 11 - Page Life Cycle in ASP.net

The document provides an overview of the ASP.NET Page Life Cycle, detailing the sequence of events that occur when a user requests a web page. It explains key concepts such as Postback, event handling, and the various stages of the page life cycle, including initialization, loading, rendering, and unloading. Additionally, it includes code examples to illustrate how to implement these concepts in ASP.NET applications.

Uploaded by

Alishba Haroon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views16 pages

Lect 11 - Page Life Cycle in ASP.net

The document provides an overview of the ASP.NET Page Life Cycle, detailing the sequence of events that occur when a user requests a web page. It explains key concepts such as Postback, event handling, and the various stages of the page life cycle, including initialization, loading, rendering, and unloading. Additionally, it includes code examples to illustrate how to implement these concepts in ASP.NET applications.

Uploaded by

Alishba Haroon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Visual Programming

Visual Programming

Page Life Cycle in ASP.NET

Each Stage In The ASP.NET Page Lifecycle

By:
Zohair Ahmed

www.youtube.com/@zohairahmed007

www.youtube.com/@zohairahmed007 www.youtube.com/@zohairahmed007
Subscribe
www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007


www.begindiscovery.com

www.begindiscovery.com www.begindiscovery.com
Overview of ASP.NET Page Life Cycle
• The page life cycle is the sequence of events that occur when a user requests an ASP.NET Web
Forms page.

• The main goal of understanding the life cycle is to know where and how to handle different events
such as initializing controls, loading data, handling user interactions, rendering content, and saving
state.

www.youtube.com/@zohairahmed007

www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007 www.begindiscovery.com


Postback
• In ASP.NET Web Forms, Postback refers to the process where a web page sends data back to the server for processing.
• After the server processes the request, the page is reloaded or "posted back" to the browser.
• This mechanism allows a web page to retain the current state of controls (like form fields) and respond to user interactions.
• Normal Page Request (Non-Postback):
‒ When a user first requests a page by typing its URL or clicking a link, it is a normal HTTP request.
‒ The page is loaded and sent to the browser for the first time.
‒ Example: Visiting MyPage.aspx for the first time.
• Postback:
‒ When a user interacts with a control (like clicking a button or changing a dropdown) that triggers an event, the page is sent
back to the server for processing.
‒ This is called a Postback because the page "posts" data back to itself.
‒ After the server processes the event (e.g., executes the button click logic), the page is re-rendered and sent back to the
browser.

www.youtube.com/@zohairahmed007

www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007 www.begindiscovery.com


Key Features of Postback
• Triggered by Server-Side Controls:

‒ Postback is initiated when server-side controls like buttons, dropdowns, or other inputs trigger
an event.

• Retains State:

‒ Postback retains the state of controls using mechanisms like ViewState. For example, if a text
box had text entered by the user, it will still have the same text after the postback.

• Server-Side Processing:

‒ Postback allows ASP.NET to handle server-side logic, such as database queries or complex
calculations, in response to user actions.

www.youtube.com/@zohairahmed007

www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007 www.begindiscovery.com


How to Detect a Postback in ASP.NET
• ASP.NET provides a property called IsPostBack to check whether the page is being loaded for the
first time or as a result of a postback.

protected void Page_Load(object sender, EventArgs e)


{
if (!IsPostBack)
{
// Code for first-time page load (e.g., populate dropdown)
}
else
{
// Code for handling postback (e.g., process button clicks)
}
}
www.youtube.com/@zohairahmed007

www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007 www.begindiscovery.com


1. Page Request
• When a user requests a page (e.g., MyPage.aspx), ASP.NET starts processing the request.

• If caching is enabled, the page may be fetched from the cache.

• Otherwise, the page is created dynamically by the server.

www.youtube.com/@zohairahmed007

www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007 www.begindiscovery.com


2. Page Initialization (Init)
• This is the first event in the life cycle.

• During initialization, controls on the page are initialized, and their properties (like ID) are set.

• The controls don’t have any content at this point.

• Code Example:

protected void Page_Init(object sender, EventArgs e)


{
// Initialize controls or set properties here
}

www.youtube.com/@zohairahmed007

www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007 www.begindiscovery.com


3. Page Load (Load)
• The Load event is where the page’s controls are populated with data.
• If it’s a postback (i.e., the page was previously submitted), the values of controls are restored.
• If it’s the first time the page is loaded, no data is available yet.
• Code Example:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Load data for first-time page load
}
}
www.youtube.com/@zohairahmed007

www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007 www.begindiscovery.com


4. Postback Event Handling
• If the page is a postback (i.e., the user has triggered an event like clicking a button), the events
related to that control are handled here.

• For example, button clicks or dropdown selections.

• Code Example:

protected void Button1_Click(object sender, EventArgs e)


{
// Handle button click event
}

www.youtube.com/@zohairahmed007

www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007 www.begindiscovery.com


5. Page PreRender (PreRender)
• Occurs just before the page is rendered to the browser.

• This is where you can make any final changes to the controls or page before rendering the HTML
output.

• Code Example:

protected void Page_PreRender(object sender, EventArgs e)


{
// Final changes to controls before rendering
}

www.youtube.com/@zohairahmed007

www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007 www.begindiscovery.com


6. Save State (SaveState)
• At this point, the state of controls is saved using ViewState.

• The ViewState mechanism is used to store control data (like text box values) across postbacks.

www.youtube.com/@zohairahmed007

www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007 www.begindiscovery.com


7. Page Rendering (Render)
• Each control generates its own HTML markup in the Render phase.

• The page’s final HTML is assembled and sent to the client’s browser.

• Code Example:

protected void Page_Render(object sender, EventArgs e)


{
// Render controls and page output
}

www.youtube.com/@zohairahmed007

www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007 www.begindiscovery.com


8. Page Unload (Unload)
• After the page is rendered, the Unload event is triggered.

• This is the last event in the lifecycle where you can clean up resources.

• At this point, the page and its controls are unloaded from memory.

• Code Example:

protected void Page_Unload(object sender, EventArgs e)


{
// Clean up resources if necessary
}

www.youtube.com/@zohairahmed007

www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007 www.begindiscovery.com


Complete Page Life Cycle
Event When Purpose Common Use Cases
Before Page_Init, first step in the Initialize page-level settings (before controls Dynamically assign master page, theme,
Page_PreInit
lifecycle are created) or settings.
After Page_PreInit, before Initialize controls and set properties of Initialize controls like TextBox,
Page_Init
controls are loaded controls DropDownList, etc.
After Page_Init, after controls Signals that the initialization of controls is
Page_InitComplete Custom logic after controls initialization.
are initialized complete
After Page_InitComplete, before Set values that affect control data before Set control values before data binding
Page_PreLoad
Page_Load loading data (e.g., setting TextBox text).
After Page_PreLoad, during the Main processing logic, data-binding, or Handle user interactions, bind data, check
Page_Load
request cycle handling user events IsPostBack.
After Page_Load, before Perform any logic that must occur after
Page_LoadComplete Signals the completion of the loading phase
rendering loading controls.
Before the page is rendered to Perform final adjustments to controls before Modify controls’ properties (e.g., visibility,
Page_PreRender
the browser rendering HTML styles).
After Page_PreRender, just Signals that the pre-rendering phase is (Rarely used) Custom logic after pre-
Page_PreRenderComplete
before rendering complete render phase.
After Page_PreRender, before ViewState is saved here, you can inspect it
Page_SaveStateComplete Finalize saving the view state of the page
rendering or add custom logic.
After all processing, during Generate the HTML markup for the page and Custom rendering logic, like altering the
Page_Render
rendering send it to the browser HTML before sending to browser.
After the page is rendered and Release database connections, file
Page_Unload Clean up any resources used by the page
the response is sent handles, or other resources.
www.youtube.com/@zohairahmed007

www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007 www.begindiscovery.com


Code Example for Each Life Cycle Event
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PageLifeCycle.aspx.cs" Inherits="WebApplication.PageLifeCycle" %>

<!DOCTYPE html>
<html>
<head runat="server">
<title>Page Life Cycle</title>
</head>
<body>
<form id="form1" runat="server">
<h1>ASP.NET Page Life Cycle Example</h1>
<div>
<p id="lblPreInit" runat="server"></p>
<p id="lblInit" runat="server"></p>
<p id="lblLoad" runat="server"></p>
<p id="lblPreRender" runat="server"></p>
<p id="lblRender" runat="server"></p>
<p id="lblUnload" runat="server"></p>
</div>
</form>
</body>
</html>
www.youtube.com/@zohairahmed007

www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007 www.begindiscovery.com


Code Example for Each Life Cycle Event
using System;
// PreRender: The page is almost ready to be shown to the user
namespace WebApplication protected void Page_PreRender(object sender, EventArgs e)
{ {
public partial class PageLifeCycle : System.Web.UI.Page lblPreRender.Text = "PreRender: The page is almost ready to be displayed.";
{ }
// PreInit: Before the page is initialized
protected void Page_PreInit(object sender, EventArgs e) // Render: The page is being turned into HTML
{ protected override void Render(System.Web.UI.HtmlTextWriter writer)
lblPreInit.Text = "PreInit: The page is being initialized."; {
} lblRender.Text = "Render: The page is being converted to HTML.";
base.Render(writer);
// Init: The page is initializing controls (like buttons or text boxes) }
protected void Page_Init(object sender, EventArgs e)
{ // Unload: Cleanup after the page is rendered
lblInit.Text = "Init: Controls are being initialized."; protected void Page_Unload(object sender, EventArgs e)
} {
lblUnload.Text = "Unload: The page is being unloaded, cleaning up resources.";
// Load: The page is loading data (for the first time or after a postback) }
protected void Page_Load(object sender, EventArgs e) }
{ }
lblLoad.Text = "Load: The page is loading data.";
}

www.youtube.com/@zohairahmed007

www.begindiscovery.com

By : Zohair Ahmed www.youtube.com/@zohairahmed007 www.begindiscovery.com

You might also like