BCA Sem 5 | Unit 1 | ASP.NET
ASP.NET Page Life Cycle
What really happens from the moment a page loads to when it's destroyed?
Every time you request an ASP.NET page (like Default.aspx), the server doesn't just "show" it directly. It goes through a fixed sequence of stages — creating controls, loading data, handling events, rendering HTML, and finally cleaning up memory.
This entire sequence is called the Page Life Cycle. Understanding it helps you know exactly when to write your code — for example, where to load data, or where to save user input.
🏢 Application Life Cycle
Happens once — when the ASP.NET application itself starts (first user visits) and ends (app stops). Defined in Global.asax.
📄 Page Life Cycle
Happens every single time a page is requested — from page request to page unload. This is what we'll explore in detail below.
Page Life Cycle — High Level Flow
Request
Events
Page Request
ASP.NET checks if the page needs to be parsed/compiled, or if a cached version can be sent directly. This happens before the actual page life cycle starts.
Before Life Cycle BeginsStart
Page properties like Request and Response are set. ASP.NET also determines whether the request is a Postback (page submitted again) or a fresh request, using Page.IsPostBack.
Page Initialization (Init)
All controls on the page (TextBox, Button, etc.) are created and get their default property values. Each control's UniqueID is set. ViewState is NOT yet available here.
Load
ASP.NET loads the ViewState and control values into the page. If it's a postback, the previous values entered by the user (like textbox text) are restored here.
Event: Page_LoadPostback Event Handling
If the user clicked a button or triggered an action (postback), the related event handler runs now — e.g. btnLogin_Click. This is where most of your business logic runs.
Rendering
ASP.NET converts all server controls into plain HTML. ViewState is also saved into a hidden field so it can be restored on the next postback.
Event: Page_RenderUnload
The page has been fully sent to the browser. ASP.NET now performs cleanup — closing files, database connections, and releasing resources from memory.
Event: Page_UnloadPage Destroyed
The page object is removed from server memory completely. The cycle is now finished — until the next request arrives.
End of Life Cycle| Order | Event Name | What Happens |
|---|---|---|
| 1 | PreInit | Check IsPostBack, set master page & theme |
| 2 | Init | Controls are initialized with default values |
| 3 | InitComplete | ViewState tracking begins |
| 4 | Load View State | ViewState data is loaded into controls |
| 5 | Load Postback Data | Form data submitted by user is processed |
| 6 | Load | Page_Load runs — most common place to write code |
| 7 | Raise Postback Events | Button click / event handlers execute |
| 8 | PreRender | Final changes before rendering to HTML |
| 9 | Save State | ViewState is saved into hidden field |
| 10 | Render | HTML is generated and sent to browser |
| 11 | Unload | Cleanup — page removed from memory |
Here's how you can actually see the life cycle events firing in order:
IsPostBack matters: Without checking if (!IsPostBack), your code (like loading data from database) would run every single time the page reloads — even on a simple button click! This wastes resources and can cause bugs.
| Reason | Details |
|---|---|
| 🐞 Debugging | Helps you find exactly where a bug happens — wrong stage = wrong data. |
| ⚡ Performance | Avoid loading data repeatedly using IsPostBack checks. |
| 🎓 Exam Important | Frequently asked in BCA Sem 5 exams and vivas. |
| 🛠️ Better Code | Know exactly when to initialize controls vs handle events. |
- Page Life Cycle = sequence of steps a page goes through on every request.
- Two types: Application Life Cycle (once) and Page Life Cycle (every request).
- Main stages: Init → Load → PreRender → Unload.
- Page_Init: Controls get default values, no ViewState yet.
- Page_Load: ViewState restored, most code is written here.
- IsPostBack property tells if the page is a fresh load or a postback.
- PreRender: Last chance to make changes before HTML is generated.
- Render: Converts controls into HTML output.
- Unload: Cleanup stage — closes connections, frees memory.
- Understanding life cycle helps avoid bugs and improves performance.
