When you are using AJAX in asp.net, server side page goes through its normal page execution lifecycle when you perform an asynchronous post back. The Page PreInit, Init, Load and PreRender events are raised for an asynchronous post back as the same way as these events are raised for a normal asp.net page post back.
Here we will check these events with small example.
Create simple Asp.Net web application and add below code in .aspx code.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>AJAX Server Side Page Execution Life Cycle Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="scriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="upDatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="btn" Text="Click for Ajax Server side Life Cycle" runat="server" />
<asp:BulletedList id="bulLog" Runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
Add below events in code behind file.
public partial class _Default : System.Web.UI.Page
{
public ArrayList _log = new ArrayList();
void Page_PreInit()
{
_log.Add("PreInit: " + scriptManager1.IsInAsyncPostBack);
}
void Page_Init()
{
_log.Add("Init: " + scriptManager1.IsInAsyncPostBack);
}
void Page_Load()
{
_log.Add("Load: " + scriptManager1.IsInAsyncPostBack);
}
void Page_PreRender()
{
_log.Add("PreRender: " + scriptManager1.IsInAsyncPostBack);
//display AJAX server side log
bulLog.DataSource = _log;
bulLog.DataBind();
}
}
Here we are testing The Page PreInit, Init, Load and PreRender events. On first page load, you will see the output as shown below.
That means, on first page load scriptManager controlIsInAsyncPostBack event is not raised, so it display as false.
Once you click on the button the out put will display as shown below.
After post back scriptManager controlIsInAsyncPostBack event is raised because button is inside the update panel control, so it display as true. Here IsInAsyncPostBack value inside the Page_PreInit is false because this event is raised during an asynchronous post back and IsInAsyncPostBack property is updated after this event. So it displayed as wrong value.