Two articles How postback events work in ASP.NET and How to handle web control events with AutoPostBack in ASP.NET provide important information about how web control events work in ASP.NET world, but you need to have a strong understating of the page life cycle too.  For example when a user changes a control that has the AutoPostBack property set to true, the following actions take place:

1. On the client side, the JavaScript __doPostBack function is executed, and the page is resubmitted to the server.

2. ASP.NET re-creates the Page object using the .aspx file.

3. ASP.NET retrieves state information from the hidden view state field and updates the controls accordingly.

4. The Page.Load event is fired.

5. The appropriate change event is fired for the control. (If more than one control has been changed, the order of change events is undetermined.)

6. The Page.PreRender event fires, and the page is rendered (transformed from a set of objects to an HTML page).

7. Finally, the Page.Unload event is fired.

8. The new page is sent to the client.

 

You can watch these events in action, by creating a simple event tracking application which writes a new entry to a list control every time one of the events it’s monitoring occurs. You can use it to see the order in which events are triggered.  The next picture presents what the window looks like after it’s been loaded once, posted back (when the text box content was changed), and posted back again (when the check box state was changed):

 

Event tracker in action in C#

Event tracker in action in C#

EventTracker.aspx

 

<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”EventTrackerVC.aspx.cs”

Inherits=”EventTrackerVC.EventTrackerVC” %>

 

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

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

<head runat=”server”>

<title>Event Tracker</title>

</head>

<body>

<form id=”Form1″ runat=”server”>

<div>

<h1>

Controls being monitored for change events:</h1>

<asp:TextBox ID=”txtBox” runat=”server” AutoPostBack=”true” OnTextChanged=”CtrlChanged” />

<br />

<br />

<asp:CheckBox ID=”chkBox” runat=”server” AutoPostBack=”true” OnCheckedChanged=”CtrlChanged” />

<br />

<br />

<asp:RadioButton ID=”rb1″ runat=”server” GroupName=”Sample” AutoPostBack=”true” OnCheckedChanged=”CtrlChanged” />

<asp:RadioButton ID=”rb2″ runat=”server” GroupName=”Sample” AutoPostBack=”true” OnCheckedChanged=”CtrlChanged” />

<h1>

List of events:</h1>

<asp:ListBox ID=”lstEvents” runat=”server” Width=”355px” Height=”150px” />

<br />

<br />

<br />

<br />

</div>

</form>

</body>

</html>

 

 

EventTracker.aspx.cs

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

namespace EventTrackerVC

{

public partial class EventTrackerVC : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

Log(“<< Page_Load >>”);

}

protected void Page_PreRender(object sender, EventArgs e)

{

// When the Page.PreRender event occurs, it is too late

// to change the list.

Log(“Page_PreRender”);

}

protected void CtrlChanged(Object sender, EventArgs e)

{

// Find the control ID of the sender.

// This requires converting the Object type into a Control class.

string ctrlName = ((Control)sender).ID;

Log(ctrlName + ” Changed”);

}

private void Log(string entry)

{

lstEvents.Items.Add(entry);

// Select the last item to scroll the list so the most recent

// entries are visible.

lstEvents.SelectedIndex = lstEvents.Items.Count – 1;

}

 

}

}