oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Debugging ASP.NET

by Wei-Meng Lee

Those of you who have developed web applications using ASP before know how cumbersome debugging your application is. In ASP, debugging in a painful process that usually involves using the Response.Write() method to output the values of variables. Ask yourself this: how many times have you forgotten to remove the debugging statements prior to deploying a web application?

Things have changed drastically with the launch of the .NET framework. In .NET, you can use the debugger in Visual Studio .NET to trace through the execution of your web application, or you can use the Trace class in the System.Web.TraceContext namespace. This article shows you how to use the Trace class to aid in your debugging efforts.

Using the Trace Class

ASP.NET includes the Trace class to help trace the flow of an application. Instead of using the Response object for debugging, you can now use the Trace class to print out debugging information.

To illustrate this, let's create an ASP.NET web application and drag-and-drop a Button and a ListBox control onto the default WebForm1 (see Figure 1). Populate the ListBox control with three items and set the AutoPostBack property to True.

Figure 1. Populating the default WebForm1

For this article, I would like to trace the execution flow of the application. First, to activate the trace, the page directive needs to have a Trace attribute with its value set to true (switch to View HTML Source mode), as shown in Figure 2.

Figure 2. Enabling tracing

Next, I injected Trace statements into the Form load event so that I know whether a postback has occurred. The PostBack event is one of the confusing aspects of ASP.NET that often trips up a beginning ASP.NET developer.

  Private Sub Page_Load(ByVal sender As System.Object, _
                        ByVal e As System.EventArgs) _
                        Handles MyBase.Load
    'Put user code to initialize the page here
    Trace.Write("Page loaded")
    If Not IsPostBack Then
      Trace.Write("Not in a postback")
      ' do something when a postback occurs

      Trace.Write("In a postback")
      ' do something...

    End If
  End Sub

I am also interested to know if a ListBox postback has occurred when a ListBox item is selected.

  Private Sub ListBox1_SelectedIndexChanged(ByVal sender As _
                   System.Object, _
                   ByVal e As System.EventArgs) Handles _
    Trace.Write("Listbox postback")
  End Sub

When the ASP.NET application is executed, the following output is shown (see Figure 3).

Figure 3. Displaying the trace information(click for larger image)

As you can see, when WebForm1 is loaded for the first time, you should see the strings "Page loaded" and "Not in a postback". If you click on the Button control on WebForm1, you should see the entry shown in Figure 4. Similarly, when the ListBox is clicked, the string "Listbox postback" will also be displayed.

Figure 4. Examining the trace information

The trace page contains the following sections (not all are shown in Figure 3):

Sections Description
Request Details Describes information pertaining to the request, e.g. SessionID, Encoding and time of request.
Trace Information Contains detailed information about the application currently running. Trace information is displayed in this section.
Control Tree Displays information about controls used in a page and the size of the Viewstate hidden field.
Cookies Collection Displays the cookie set by the page and its value.
Headers Collection Displays HTTP header information, such as content length and user agent.
Forms Collection Displays the name of controls in a page and their values.
Server variables Displays the environment variables on the server side.

Notice that our trace message is written under the Trace Information section. To turn off tracing, simply set the Trace attribute in the page directive to False. There is no need to remove the trace instructions in our application; turning off debugging is now simply a matter of setting a Boolean value!

Pages: 1, 2

Next Pagearrow