ASP.Net Page Inheritance

Inheriting directly from the base Page class (System.Web.UI.Page) is not good practice.  Instead, create a new base Page class that inherits from System.Web.UI.Page, and inherit all of your pages from your new base page!

namespace MyNamespace {... public class Page : System.Web.UI.Page{...}}
namespace MyNamespace {... public class SomePage : MyNamespace:Page{...}}

Dino Esposito has posted a MSDN article, “Build Your ASP.NET Pages on a Richer Bedrock”, that lists a few immediate examples of functionality you can add to your pages by simply modifying your base Page class. 

* Trapping the Browser Refresh - Why is this important?  End-users don't think twice about using refresh or navigating through their history.  Unfortunately, this includes moments that some data-changing postback just occurred, like:
   * A final check-out in an e-commerce app
   * Deleting a record of some type
   * Posting a message
You get the idea... refreshing a page can mean reposting the last request, which can be dangerous, and a difficult bug to kill on all pages where it wasn't handled appropriately.  With an inherited page architecture, you can trap the request, and handle it appropriately in one place!

* Status bar or message on long requests - Hourglasses are boring and annoying.  In Wireless areas, when connections are weak or intermittent, they're obnoxious.  For usability purposes, some type of status bar or message is appropriate on long requests.  These features are difficult to add individually to such requests.  Once again, inheritance comes to the rescue with a static method that takes two urls for processing: one for the status message (”Please wait...”), and the other as a destination message (”Process complete...”).

* SetFocus(), OnEnter(), Popups on page load - Dino gives an example for setting focus to a control on page load by writing the javascript from the base Page class.  This technique can be applied for any common OnLoad javascript events, to be accessible to all pages that inherit from the base page.

Note: ASP.Net 2.0 will have a SetFocus method!

I got some great ideas (as usual) from Dino's article.  ASP.Net is like any other object-oriented language: Using inheritance saves time and trouble, and enables you to write your functionality into your app once, keeping things clean and easy to manage.

Print | posted on Wednesday, September 22, 2004 9:31 AM

Feedback

# re: ASP.Net Page Inheritance

left by Alexander Popov at 3/12/2005 9:54 AM Gravatar
Hello!
I read your article about subj. id do something like of this some years ago, but have one trouble that i found in your code too.
If i open page and duplicate it by Ctrl_N. when i submit page on first window and than try submit page in second window, in second winow we can see that page IS REFRESHED.
it's some confusing.
have you any ideas?

# re: ASP.Net Page Inheritance

left by Gavin Chan at 7/7/2005 5:08 AM Gravatar
I found that the trap brwoser refresh code is broken in Windows 2003! Any idea?
Title  
Name
Email (never displayed)
Url
Comments   
Please add 6 and 3 and type the answer here: