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:
EventTracing.aspx
<%@ Page Language="C#" Inherits="AdderPage" Src="EventTracing.aspx.cs"
AutoEventWireup="true" %>
<html>
<head> <title>Kassenstand</title> </head>
<body>
<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" />
<br><br>
<asp:Label ID="trace" Runat="server"/>
</form>
</body>
</html>
|
The code-behind file contains the code that prints the trace information:
EventTracing.aspx.cs
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) {
Put("Page.PreRender");
}
public void Page_Unload(object sender, EventArgs e) {
Put("Page.Unload");
}
//------ Button events
public void ButtonClick(object sender, EventArgs e) {
Put("Button.Click");
int totalVal = Convert.ToInt32(total.Text);
int amountVal = Convert.ToInt32(amount.Text);
total.Text = (totalVal + amountVal).ToString();
}
public void ButtonInit(object sender, EventArgs e) {
Put("Button.Init");
}
public void ButtonLoad(object sender, EventArgs e) {
Put("Button.Load");
}
public void ButtonPreRender(object sender, EventArgs e) {
Put("Button.PreRender");
}
public void ButtonUnload(object sender, EventArgs e) {
Put("Button.Unload");
}
}
|
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.
http://dotnet.jku.at/book/samples/6/EventTracing.aspx
|