Lect 11 - Page Life Cycle in ASP.net
Lect 11 - Page Life Cycle in ASP.net
Visual Programming
By:
Zohair Ahmed
www.youtube.com/@zohairahmed007
www.youtube.com/@zohairahmed007 www.youtube.com/@zohairahmed007
Subscribe
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
www.youtube.com/@zohairahmed007
www.begindiscovery.com
‒ 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
www.begindiscovery.com
www.youtube.com/@zohairahmed007
www.begindiscovery.com
• During initialization, controls on the page are initialized, and their properties (like ID) are set.
• Code Example:
www.youtube.com/@zohairahmed007
www.begindiscovery.com
www.begindiscovery.com
• Code Example:
www.youtube.com/@zohairahmed007
www.begindiscovery.com
• This is where you can make any final changes to the controls or page before rendering the HTML
output.
• Code Example:
www.youtube.com/@zohairahmed007
www.begindiscovery.com
• The ViewState mechanism is used to store control data (like text box values) across postbacks.
www.youtube.com/@zohairahmed007
www.begindiscovery.com
• The page’s final HTML is assembled and sent to the client’s browser.
• Code Example:
www.youtube.com/@zohairahmed007
www.begindiscovery.com
• 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:
www.youtube.com/@zohairahmed007
www.begindiscovery.com
www.begindiscovery.com
<!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
www.youtube.com/@zohairahmed007
www.begindiscovery.com