All You Need to Know About ASP.NET Life Cycle

ASP.NET is the most popular technology for developing web applications today. There are several steps involved in an application development’s life cycle. Here in this tutorial, you will find all the steps needed to build an application.

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

What Is ASP.NET?

ASP.NET is a server-side technology used for developing dynamic websites and web applications. It enables developers to create web applications using HTML, CSS, and JavaScript.

ASP.NET_logo.

ASP.NET is the latest version of Active Server Pages, which Microsoft developed to build websites. It is a web application framework released in 2002 and has an extension of .aspx.  

Some of the major companies that use ASP.NET are:

  • Facebook

  • Instagram

  • Twitter
  • Email

  • Youtube

LifeCycle_of_ASP.NET_What_is_ASP.NET

Since you now have been shown a little about the basics of ASP.NET, it is time to investigate the life cycle of ASP.NET.

Full Stack Java Developer Course

In Partnership with HIRIST and HackerEarthEXPLORE COURSE
Full Stack Java Developer Course

Life Cycle of ASP.NET

The ASP.NET life cycle is very crucial to developing applications. It includes various stages that help to produce dynamic output.

Life_Cycle_Of_ASP.NET_Lifecycle

The Life Cycle of ASP.NET is divided into two categories:

  1. Application Life Cycle

  2. Page Life Cycle

Application Life Cycle

The application life cycle contains the following steps:

What_is_Application_Lifecycle.

1. Application Start

The webserver executes the application start when a user requests an application for access. In this method, it sets all global variables to default.

2. Object Creation

Object creation holds all the HTTP Context, HTTP Request, and HTTP Response by the webserver. It also contains information about the request, cookies, and browsing information.

3. HTTP Application

HTTP Application is an object created by the webserver. It helps to process all the subsequent information that is sent to the user.

4. Dispose

Dispose is an event that is called before the application is destroyed. It also helps to release manually unmanaged resources when the objects are no longer needed.

5. Application End

Application End is the last stage of the application life cycle. It helps to unload the memory.        

FREE Java Certification Training

Learn A-Z of Java like never beforeEnroll Now
FREE Java Certification Training

Page Life Cycle

The Page Life Cycle has certain phases that help in writing custom controls and initializing an application.

Following are the different phases of the Page Life Cycle:

What_is_Page_Lifecycle

1. Page Request

Page Request is the first step of the page life cycle. When a user request is made, the server checks the request and compiles the pages. Once the compilation is done, the request is sent back to the user.

2. Page Start

Page Start helps in creating two objects: request and response. The request holds all the information which the user sent, while the response contains all the information that is sent back to the user.

3. Page Initialization

Page Initialization helps to set all the controls on the pages. It has a separate ID, and it applies themes to the pages in this step.

4. Page Load

Page Load helps to load all the control properties of an application. It also helps to provide information using view state and control state.

5. Validation

Validation happens when the execution of an application is successful. It returns two conditions: true and false. If execution is successful, it returns true, otherwise false.

6. Event Handling

Event Handling takes place when the same pages are loaded. It is a response for the validation. When the same page is loaded, a Postback event is called, which checks the page’s credentials.

7. Rendering

Rendering happens before it sends all the information back to the user. And all this information is stored before being sent back.

8. Unload

Unload is a process that helps in deleting all unwanted information from the memory once the output is sent to the user.

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

Page Life Cycle Events

Here’s a collection of typical page life cycle events you may encounter.

  • PreInit: This event occurs when the start stage is done but before the initialization stage.
  • Init: This event reads and initializes control properties and happens after all the controls are initialized.
  • InitComplete: This happens at the conclusion of the page’s initialization stage. The event makes changes to the view state that we want to ensure are persisted at the end of the subsequent postback.
  • PreLoad: PreLoad occurs at the conclusion of the event-handling stage, and is used for tasks that need all other page controls to be loaded.
  • Load: Load is raised initially for the page, then recursively for the child controls.
  • Control events: This takes care of handling specific control events like button control clicks.
  • LoadComplete: This event is used for many tasks that need other controls found on the page to be loaded. LoadComplete occurs at the conclusion of the event-handling stage.
  • PreRender: This event kicks in after the page object has created the controls necessary to render the page.
  • PreRenderComplete: This event happens once the pre-render stage is complete.
  • SaveStateComplete: This event is raised when the view and control states are saved for both the page and all the controls.
  • Render: Render isn’t actually an event; rather, it’s a method that the Page object calls for each control.
  • Unload: This event is first raised for each control, then for the page itself.

Catch-Up Events for Added Controls

Should the controls be created dynamically at run time or created declaratively within the data-bound control templates, their respective events aren’t initially synchronized with the rest of the page’s controls. So, dynamically added controls and controls in templates must raise their events sequentially until all of them have caught up to the event present during its addition to the controls collection.

Data Binding Events for Data-Bound Controls

Here’s a chart that shows data-related events within the context of data-bound controls.

Data-Related Control Event

Usual Use

DataBinding

Marks the start of the process which binds the data with the control. This event is typically used to dynamically set parameter values or manually open the database connections.

RowCreated (for GridView only) or ItemCreated (for DataList, DetailsView, SiteMapPath, DataGrid, FormView, Repeater, and ListView controls)

This event gets raised after the control’s DataBinding event and used to manipulate any content that isn’t dependent on data binding.

RowDataBound (for GridView only) or ItemDataBound (for DataList, SiteMapPath, DataGrid, Repeater, and ListView controls)

This event is raised either after the control’s ItemCreated or RowCreated events. It means the data is now available in the row or the item, so it can be formatted or have the FilterExpression property set on a child data source.

DataBound

You use this event to format any data-bound content or start data binding in other controls that rely on values derived from the current control’s content.

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

Nested Data-Bound Controls

When a child control is data bound, but its container control isn’t yet, this potentially causes the child control data and the container control data to end up out of sync. As a result, you must embed controls within a template.

For example, you can use a Repeater control to connect various types of databases, extract the information, and present it in a clear, organized format on webpages. There are other data bound controls like DataGrid, DataList, and GridView.

Here’s an example using the Repeated control, as shown courtesy of Codeproject.com:

ASP.NET

Shrink ▲   Copy Code

<asp:Repeater id="RepeaterAdd" runat="server">

<ItemTemplate>

<div>

      <asp:Label ID="lblHead" runat="server" CssClass="level2">

           <%# DataBinder.Eval(Container.DataItem, "LKI_NAME") %>

                Addresses:

      </asp:Label>

</div>

//Here LKI_NAME contain Address Types description like "Home",

// "Office" etc     <br />

//Here I have nested Child Repeater control inside my main repeater.

// Main repeater "RepeaterAdd" contains address types while repeater

// "Rchild" contains address related to each address type.

<asp:Repeater ID="Rchild" runat="server">

<ItemTemplate>

     <asp:Label ID="Label1" runat="server" CssClass="modulebox"

         Width="100">Address Line1:</asp:Label> 

     <asp:Label ID="lbl1" runat="server" CssClass="moduleBoxBlack">

         <%# DataBinder.Eval(Container.DataItem, "ADR_ADDRESSL1") %>

     </asp:Label>

     <br />

         <asp:Label ID="Label2" runat="server" CssClass="modulebox"

             Width="100">Address Line2:</asp:Label></b>  

         <asp:Label ID="lbl2" runat="server" CssClass="moduleBoxBlack">

             <%# DataBinder.Eval(Container.DataItem, "ADR_ADDRESSL2")%>

         </asp:Label>

     <br />

         <asp:Label ID="Label4" runat="server" CssClass="modulebox"

             Width="100">City Name:</asp:Label> 

         <asp:Label ID="lbl4" runat="server" CssClass="moduleBoxBlack">

             <%# DataBinder.Eval(Container.DataItem, "ADR_CITYNAME") %>

         </asp:Label>

     <br />

         <asp:Label ID="Label3" runat="server" CssClass="modulebox"

             Width="100">State:</asp:Label> 

         <asp:Label ID="lbl3" runat="server" CssClass="moduleBoxBlack">

             <%# DataBinder.Eval(Container.DataItem, "ADR_STATENAME")%>

         </asp:Label>

     <br />

         <asp:Label ID="Label5" runat="server" CssClass="modulebox"

             Width="100">Postal Code:</asp:Label> 

         <asp:Label ID="lbl5" runat="server" CssClass="moduleBoxBlack">

             <%# DataBinder.Eval(Container.DataItem,"ADR_POSTALCODE")%>

         </asp:Label>

     <br />

         <asp:Label ID="Label6" runat="server" CssClass="modulebox"

             Width="100">Country:</asp:Label>         

         <asp:Label ID="lbl6" runat="server" CssClass="moduleBoxBlack">

             <%# DataBinder.Eval(Container.DataItem, "CNT_NAME") %>

         </asp:Label>

         <asp:LinkButton Runat="server" ID="LnkEdit"

             CssClass="LinkBtn">Edit</asp:LinkButton>

                

         <asp:LinkButton Runat="server" ID="lnkDelete"

             CommandName="DeleteMe"

             CommandArgument='<%# DataBinder.Eval(Container.DataItem,

             "ADR_ID") %>' CssClass="LinkBtn">Delete</asp:LinkButton>

     <br /><br />

</ItemTemplate>

</asp:Repeater><br /> 

<asp:Button ID="btnAdd" Text="Add" runat="server"

      CssClass="button" CommandName="OpenPopup" />

<br /><br /><br />    

</ItemTemplate></asp:Repeater>

Caltech Coding Bootcamp

Become a full stack developer in 6 monthsEnroll Now
Caltech Coding Bootcamp

Login Control Events

Although the Login control can employ the settings in the Web.config file to automatically manage the membership authentication, you can use these events to better understand how the Login control events will relate to the page’s life cycle, or if you want to customize the controls.

Control Event

Its Typical Use

LoggingIn

It makes the start of the login process and used for tasks that must happen before the authentication process begins.

Authenticate

This event gets raised after the LoggingIn event, used to override or enhance a Login control’s default authentication behavior.

LoggedIn

Raised after successful authentication of the username and password. The event redirects to another page or dynamically sets the control’s text. However, if authentication fails or an error occurs, the event won’t happen.

LogInError

If the authentication didn’t work, use this event to that either set control text to explain the issue or redirect the user to another page.

ASPNet Life Cycle Examples

Here are a few examples, for illustrative purposes, of some of the aspects of the ASPNet Life Cycle, all provided courtesy of TechStrikers.

ASP.NET Page PreRenderComplete Event Example - Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"

Inherits="_Default" %>

<!DOCTYPE html">

<html xmlns="https://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

<form id="form1" runat="server">

    <div>

        <asp:Label ID="lblTitle" runat="server" Text="ASP.NET Page Life Cycle"></asp:Label>lt;br />

        <asp:label ID="lblDisplayPageEventsSteps" runat="server" text="Label"></asp:label>

    </div>

    </form>

</body>

</html>

ASP.NET PreInit Event Example - Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"

Inherits="_Default" %>

<!DOCTYPE html">

<html xmlns="https://www.w3.org/1999/xhtml">

<head runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

    <div>

        <asp:Label ID="lblTitle" runat="server" Text="ASP.NET Page Life Cycle"></asp:Label><br />

        <asp:label ID="lblDisplayPageEventsSteps" runat="server" text="Label"></asp:label>

    </div>

    </form>

</body>

</html>

ASP.NET Page Load Event Example - Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"

Inherits="_Default" %>

<!DOCTYPE html">

<html xmlns="https://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

<form id="form1" runat="server">

    <div>

        <asp:Label ID="lblTitle" runat="server" Text="ASP.NET Page Life Cycle"></asp:Label>lt;br />

     <asp:label ID="lblDisplayPageEventsSteps" runat="server" text="Label"></asp:label>

    </div>

    </form>

</body>

</html>

Get a firm foundation in Java, the most commonly used programming language in software development with the Java Certification Training Course.

Conclusion

Now you have everything you need to know about the ASP.NET Life Cycle. We’ve seen what ASP.NET is, and different companies that use ASP.NET. 

Then, you learned about the two types of life cycles in the ASP.NET: Application Life Cycle and Page Life Cycle. There, you saw the different steps and phases involved in both types of ASP.NET Life Cycle.

If you are planning to learn .NET, consider enrolling in Simplilearn’s .NET Programming course. This comprehensive program will help you master the fundamentals of .NET programming and will help you create .NET projects. In the .NET programming certification course, you will be introduced to .NET space and coding with C#, including Visual Studio and Winforms, which will help you excel in your career.

If you have any questions for us, let us know in the comment section. Our experts will answer them for you right away!

About the Author

John TerraJohn Terra

John Terra lives in Nashua, New Hampshire and has been writing freelance since 1986. Besides his volume of work in the gaming industry, he has written articles for Inc.Magazine and Computer Shopper, as well as software reviews for ZDNet. More recently, he has done extensive work as a professional blogger. His hobbies include running, gaming, and consuming craft beers. His refrigerator is Wi-Fi compliant.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.