asp.netPRO RadControls for ASP.NET AJAX



Subscription Services
Print Subscription
Online-Only Subscription
Renew Subscription
asp.netNOW Newsletter
Change of Address
Pay An Invoice
Subscription Packages

asp.netPRO
Articles
411asp.net Directory
New Products
Book Reviews
Blog Listings  
E-Newsletter Articles- NEW
Events  - NEW 
Job Listings  
Product Reviews
Opinion
Back Issues
Reprints/E-prints
Search

Downloads
Premium Downloads


Informant
Contact Us
Advertise with Us
Write For Us




 
 
 


US & Europe Data Centers – DiscountASP.NET


ASP.NET 3.5 & MS SQL 2008 – DiscountASP.NET



2007 asp.netPRO Complete Works CD
Co-Sponsored by:
Download your free trial now!

Click here for the online product directory, asp.netPRO Product Portal

Latest Features

 •

Load Balancing and Scaling Your WCF Services


 •

Got Maps?


 •

The View and the Controller


 •

Take Control


 •

Assess Your Access



Article Rating



Tell a friend
about this article!




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.

 

 

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:brad@infragistics.com.

 

 

 

 

Microsoft Internet Explorer
Top of page

RadControls for ASP.NET AJAX


Informant Communications Group

Informant Communications Group, Inc.
5105 Florin Perkins Road
Sacramento, CA 95826
Phone: (916) 379-0609 • Fax: (916) 379-0610

Copyright © 2008 Informant Communications Group. All Rights Reserved. • Site Use Agreement • Send feedback to the Webmaster • Important information about privacy