6.2. Using a Centralized Base Page

Chapter 5 states that by default all ASPX pages derive from a class called System.Web.UI.Page. This means all of your pages have at least the behavior defined in this class.

However, there are circumstances where this behavior is not enough and you need to add your own stuff to the mix. For example, you may have the need to add behavior that applies to all the pages in your site. Instead of adding this behavior to each and every individual page, you can create a common base page. All the pages in your site can then inherit from this intermediate page instead of from the standard Page class. The left half of Figure 6-7 shows how a default ASPX page called MyWebPage inherits from the Page class directly. The right half shows a situation where the ASPX page inherits from a class called BasePage, which in turn inherits from Page.

Figure 6-7. Figure 6-7

To be able to have your pages inherit from this base page, you need to do two things:

  • Create a class that inherits from System.Web.UI.Page in the App_Code folder of your web site.

  • Make the web pages in your site inherit from this base page instead of the standard Page class.

In the following exercise you create a new base page class inside the App_Code folder. For now, the sole purpose of this class is to check the Title of the page at runtime to avoid pages with the default name of Untitled Page that ...

Get Beginning ASP.NET 3.5: In C# and VB now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.