Create a Custom Page Base Class

Increase your productivity — and write less code — by moving common code into a custom page base class.

UI Tip

LANGUAGES: VB .NET

TECHNOLOGIES: Client Events | Server Controls

 

Create a Custom Page Base Class

Increase your productivity - and write less code - by moving common code into a custom page base class.

 

By Brad McCabe

 

The object-oriented features and behaviors that .NET bring to the ASP world via ASP.NET let you greatly increase your productivity while writing less code. One way to do this is by moving common code into a custom page base class.

 

All pages in ASP.NET inherit from a code-behind page, which in turn inherits from the System.Web.UI.Page class in the .NET Framework. Inside this base class is where you can insert an additional level of abstraction and extend the functionality available for every page.

 

To start, create a new class file named PageBase.VB and add set it to inherit from the system page class as follows:

 

Imports System.Text

Imports System.Diagnostics

Public Class PageBase

      Inherits System.Web.UI.Page

 

At this point, you can write any functions or routines you want to share across all of your Web pages. For this example, I override the default error handling to write the message, username, and exact URL to the Windows event log. This lets the system administrator follow up with users if repeated errors occur. It also will provide additional information should the user not report the problem. You even can extend this function to e-mail the administrator proactively and report the problem before the user reports it or logs it to a bug-tracking system.

 

First, the error handler formats the exception thrown and writes it to the event log. After your code is complete, you delegate to the base class, System.Web.UI.Page, letting ASP.NET handle the error through its standard processes:

 

Protected Overrides Sub OnError(ByVal e As EventArgs)

Dim ErrorMessage As New StringBuilder(FormatException(Server.GetLastError()))

 

      ErrorMessage.Append(ControlChars.CrLf)

      ErrorMessage.Append(ControlChars.CrLf)

      ErrorMessage.Append("UserName: ")

      ErrorMessage.Append(User.Identity.Name)

      ErrorMessage.Append(ControlChars.CrLf)

      ErrorMessage.Append("URL: ")

      ErrorMessage.Append(Request.Url)

 

      Dim EventLog As New EventLog()

      EventLog.WriteEntry(ErrorMessage.ToString)

 

      MyBase.OnError(e)

End Sub

 

Private Function FormatException(ByVal ex As Exception) As String

       With New StringBuilder()

         .Append(ex.Message)

            .Append(ControlChars.CrLf)

            .Append(ex.StackTrace)

 

            Return .ToString

      End With

End Function

 

Once you complete your custom base class, the remaining task is to change the inheritance of your ASP.NET code-behind pages to point to your new class. Once you do this, you have the full power of an ASP.NET page, plus the addition of your custom properties, methods, and code.

 

Brad McCabe is the technical evangelist for Infragistics. Brad also has been a systems architect and consultant for Verizon Communications and many other clients, and he was a leading .NET evangelist within Ajilon Consulting. His primary interests include ASP.NET, Windows CE .NET, .NET Compact Framework, and Microsoft's networking technologies. E-mail him at mailto:[email protected].

 

 

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish