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):
EventTracker.aspx
<%@ Page Language=”vb” AutoEventWireup=”false” CodeBehind=”EventTrackerVB.aspx.vb” Inherits=”EventTrackerVB.EventTrackerVB” %>
<!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.vb
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace EventTrackerVB
Public Partial Class EventTrackerVB
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs)
Log(“<< Page_Load >>”)
End Sub
Protected Sub Page_PreRender(sender As Object, e As EventArgs)
‘ When the Page.PreRender event occurs, it is too late
‘ to change the list.
Log(“Page_PreRender”)
End Sub
Protected Sub CtrlChanged(sender As [Object], e As EventArgs)
‘ Find the control ID of the sender.
‘ This requires converting the Object type into a Control class.
Dim ctrlName As String = DirectCast(sender, Control).ID
Log(ctrlName & ” Changed”)
End Sub
Private Sub Log(entry As String)
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
End Sub
End Class
End Namespace