The task is to create an ASP.NET Web Forms application that logs and displays the different stages of the ASP.NET page life cycle. The application must allow the user to click a button to either show log messages or display the sequence of events triggered during the page lifecycle.
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Page Life Cycle Logger</title>
</head>
<body>
<form id="form1" runat="server">
<div style="font-family:Arial; padding:20px;">
<h2>ASP.NET Page Life Cycle Logger</h2>
<asp:Button ID="btnShowLogs" runat="server" Text="Show Logs" />
<asp:Button ID="btnShowEvents" runat="server" Text="Show Page Life Cycle Events" />
<br /><br />
<asp:Label ID="lblOutput" runat="server" Text="" Font-Size="Small" />
</div>
</form>
</body>
</html>
Partial Class _Default
Inherits System.Web.UI.Page
Private eventLog As String = ""
Private showEvents As Boolean = False
Private showLogs As Boolean = False
Private Function Timestamp() As String
Return "[" & DateTime.Now.ToString("HH:mm:ss") & "]"
End Function
' Page lifecycle events
Protected Sub Page_PreInit(...) Handles Me.PreInit ...
Protected Sub Page_Init(...) Handles Me.Init ...
...
Protected Overrides Sub Render(writer As HtmlTextWriter)
If showEvents Or showLogs Then
lblOutput.Text = eventLog
End If
MyBase.Render(writer)
End Sub
' Button click events
Protected Sub btnShowLogs_Click(...) Handles btnShowLogs.Click ...
Protected Sub btnShowEvents_Click(...) Handles btnShowEvents.Click ...
End Class
Example 1: When "Show Page Life Cycle Events" is clicked
[12:02:10] - PreInit Stage
[12:02:10] - Init Stage
[12:02:10] - InitComplete Stage
[12:02:10] - PreLoad Stage
[12:02:10] - Load Stage
[12:02:10] - LoadComplete Stage
[12:02:10] - PreRender Stage
[12:02:10] - PreRenderComplete Stage
[12:02:10] - SaveStateComplete Stage
Example 2: When "Show Logs" is clicked
[12:03:55] - User Clicked Log Button
This lab helped in understanding the complete page life cycle in ASP.NET Web Forms. By handling and logging different events like PreInit, Init, Load, PreRender, etc., we gained practical insights into how ASP.NET processes each request. The use of ViewState for state management and server-side controls for interaction was effectively demonstrated.