There is one last topic that you must examine before completing this chapter. In previous sections, I talked about methods of developing basic controls, but I haven't discussed how you can debug a control. You can use the Trace object within a control, as shown in Listing 28.36, to display trace messages. Listing 28.36 ControlTrace.vbImports System Imports System.Web Imports System.Web.UI Namespace myControls Public Class ControlTrace Inherits Control Overrides Protected Sub OnLoad( e As EventArgs ) Context.Trace.Warn( "Executing OnLoad Method!" ) End Sub Overrides Protected Sub OnPreRender( e As EventArgs ) Context.Trace.Warn( "Executing OnPreRender Method!" ) End Sub Overrides Protected Sub Render( objTextWriter As HtmlTextWriter ) Context.Trace.Warn( "Executing Render Method!" ) objTextWriter.WriteLine( "Hello World!" ) End Sub End Class End Namespace The C# version of this code can be found on the CD-ROM. After you add trace messages to a control, you need to enable tracing for the page that contains the control. The page in Listing 28.37 illustrates how you can enable tracing and show all the trace messages from the ControlTrace control. Listing 28.37 DisplayControlTrace.aspx<%@ Page Trace="True" %> <%@ Register TagPrefix="myControls" Namespace="myControls" Assembly="ControlTrace"%> <html> <head><title>DisplayControlTrace.aspx</title></head> <body> <form Runat="Server"> <myControls:ControlTrace Runat="Server"/> </form> </body> </html> The C# version of this code can be found on the CD-ROM. When the page in Listing 28.37 is displayed, all the trace messages from the control appear at the bottom of the page in a red font (see Figure 28.4). Figure 28.4. Displaying control trace messages.
|