Home   Cover Cover Cover Cover

Event Tracing in a Web Form

From Section 6.3 of the book

This example shows which events are raised when an asp page is loaded and when a button is clicked. As a starting point we use the Adder page from Section 6.2 of the book:

<%@ Page Language="C#" Inherits="AdderPage" Src="EventTracing.aspx.cs"
  AutoEventWireup="true" %>
  <head> <title>Kassenstand</title> </head>
    <form method="post" Runat="server">
      <b>Current balance:</b>
      <asp:Label ID="total" Text="0" Runat="server"/> Dollars<br><br>
      <asp:TextBox ID="amount" Runat="server"/>
      <asp:Button ID="ok" Text="Deposit" Runat="server" />
      <asp:Label ID="trace" Runat="server"/>

The code-behind file contains the code that prints the trace information:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

public class AdderPage : Page {
  protected Label total;
  protected TextBox amount;
  protected Button ok;
  protected Label trace;

  private void Put(string s) { trace.Text += s + "<" + "br>"; }
  //------ Page events
  public void Page_Init(object sender, EventArgs e){
    Put("Page.Init ");
    ok.Load += new EventHandler(ButtonLoad);
    ok.Click += new EventHandler(ButtonClick);
    ok.PreRender += new EventHandler(ButtonPreRender);
    ok.Unload += new EventHandler(ButtonUnload);
  public void Page_Load(object sender, EventArgs e) {
    Put("<" + "br>Page.Load");
  public void Page_PreRender(object sender, EventArgs e) {
  public void Page_Unload(object sender, EventArgs e) {
  //------ Button events
  public void ButtonClick(object sender, EventArgs e) {
    int totalVal = Convert.ToInt32(total.Text);
    int amountVal = Convert.ToInt32(amount.Text);
    total.Text = (totalVal + amountVal).ToString();
  public void ButtonInit(object sender, EventArgs e) {
  public void ButtonLoad(object sender, EventArgs e) {
  public void ButtonPreRender(object sender, EventArgs e) {
  public void ButtonUnload(object sender, EventArgs e) {

Try it

Click on the link below and then on the button on the new web page. The page will show which events are raised.