Lab Report: Page Life Cycle Tracker using ASP.NET Web Forms

Introduction

This lab project demonstrates the ASP.NET Web Forms page life cycle by implementing a simple web application that logs various page lifecycle events.

Objective

Frontend Code (Default.aspx)

<%@ Page Language="vb" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="LifecycleLogger" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Page Life Cycle Tracker</title>
</head>
<body>
    <form id="mainForm" runat="server">
        <div>
            <asp:Button ID="btnLogCycle" runat="server" Text="Track Life Cycle" OnClick="btnLogCycle_Click" />
            <br /><br />
            <asp:Button ID="btnDisplayLog" runat="server" Text="Display Log" OnClick="btnDisplayLog_Click" />
            <br /><br />
            <asp:Literal ID="litCycleOutput" runat="server" />
        </div>
    </form>
</body>
</html>
        

Code-Behind (Default.aspx.vb)

The code-behind handles:

Partial Class LifecycleLogger
    Inherits System.Web.UI.Page

    Private Property LifecycleHistory As String
        Get
            Return If(ViewState("LifecycleHistory"), String.Empty)
        End Get
        Set(ByVal value As String)
            ViewState("LifecycleHistory") = value
        End Set
    End Property

    Private Sub RecordEvent(ByVal stageName As String)
        LifecycleHistory &= stageName & "<br/>"
    End Sub

    ' Lifecycle Events
    Protected Sub Page_PreInit(...) Handles Me.PreInit : RecordEvent("Page_PreInit") : End Sub
    Protected Sub Page_Init(...) Handles Me.Init : RecordEvent("Page_Init") : End Sub
    Protected Sub Page_InitComplete(...) Handles Me.InitComplete : RecordEvent("Page_InitComplete") : End Sub
    Protected Sub Page_PreLoad(...) Handles Me.PreLoad : RecordEvent("Page_PreLoad") : End Sub
    Protected Sub Page_Load(...) Handles Me.Load : RecordEvent("Page_Load") : End Sub
    Protected Sub Page_LoadComplete(...) Handles Me.LoadComplete : RecordEvent("Page_LoadComplete") : End Sub
    Protected Sub Page_PreRender(...) Handles Me.PreRender : RecordEvent("Page_PreRender") : End Sub
    Protected Sub Page_PreRenderComplete(...) Handles Me.PreRenderComplete : RecordEvent("Page_PreRenderComplete") : End Sub
    Protected Sub Page_SaveStateComplete(...) Handles Me.SaveStateComplete : RecordEvent("Page_SaveStateComplete") : End Sub
    Protected Sub Page_Unload(...) Handles Me.Unload : ' Do not modify UI in Unload' : End Sub

    ' Button Click Events
    Protected Sub btnLogCycle_Click(...) : RecordEvent("btnLogCycle_Click") : End Sub
    Protected Sub btnDisplayLog_Click(...) : litCycleOutput.Text = LifecycleHistory : End Sub
End Class
        

Conclusion

This project demonstrates the full ASP.NET Web Forms page lifecycle and how developers can use events and ViewState to track and persist data across postbacks. It serves as an essential foundation for understanding how server-side page rendering and postback works in .NET web applications.

Web hosting by Somee.com