ASP.NET Tip: Create a BasePage Class for All Pages to Share Portland OR

Creating a shared Page class from which all your Web pages inherit allows the pages to share common functions, settings, and so forth without having to do much work.

Local Companies

Cascade Custom Software
503-922-0135
1000 SW Broadway
Portland, OR
Advantyx Software LLC
503-246-9299
6501 SW Macadam Ave
Portland, OR
Aravo Solutions
503-224-4049
2627 NW Nicolai St
Portland, OR
Axis Clinical Software Inc
503-292-3022
6443 SW Beaverton Hillsdale Hwy
Portland, OR
ApogeeInvent
403.407.9915
9999 SW Wilshire St. Ste. 100
Portland, OR
Cendix
503.789.2676
501 4th Street, Suite 741
Lake Oswego, OR
Artisan Software Tools Inc
503-245-6200
10220 SW Greenburg Rd
Portland, OR
Vision33
971-255-0162
The Lincoln Center, 10260 SW Greenburg Road, 4th Floor
Portland, OR
Paradigma Software
(503) 574-2776
6107 SW Murray Blvd #151
Beaverton, OR
ClearStar.net
360-892-0687
915 Broadway
Vancouver, WA

provided by: 
Originally published at Internet.com


Something I've been doing with my ASP.NET applications is creating a shared Page class from which all my Web pages inherit. This allows all the pages to share common functions, settings, and so forth without having to do much work. In ASP.NET 1.1, I used the BasePage class to help control the rendering of the page, but the Master Page in ASP.NET 2.0 eliminated this requirement.

To create a BasePage, you just add a class to your project. In 2.0, this class will go into the App_Code folder. It will inherit from System.Web.UI.Page, so the beginnings of the class will look like the following (C#): public class BasePage : System.Web.UI.Page { }

Once you have the class, you'll need to change your ASP.NET pages to inherit from this class instead of directly from System.Web.UI.Page. Your revised code-behind file for a Web page will resemble the following: public partial class MyPage : BasePage { protected void Page_Load(object sender, EventArgs e) { } }

Now that you have the inheritance set up between the two files, you can add common elements to the BasePage class and make them available to the Web page. For instance, a database-driven Web application might want to automatically open a database connection on each page. You can add this code to the OnInit event of the BasePage class, like so: public class BasePage : System.Web.UI.Page { public SqlConnection ActiveConnection; protected override void OnInit(EventArgs e) { ActiveConnection = new SqlConnection(...); ActiveConnection.Open(); } }

The variable named ActiveConnection will now be a live database connection available to any Web page. You'll also want to add the corresponding Close code to the OnUnload event if you open the connection in this manner.

The other thing to remember is that any variables declared as private in the BasePage won't be visible to the Web page, and any variables declared as protected in the BasePage won't be available to the ASPX portion of the Web page, but they will be available to the code-behind part of the ASPX page.

Future tips will discuss properties that instantiate objects when they are first requested rather than every time like this code does. You may have pages where a database connection isn't always required, so this adds time that isn't necessary.

About the Author

Eric Smith is the owner of Northstar Computer Systems, a Web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic.

Author: Eric Smith

Read article at Internet.com site

Featured Local Company

Cascade Custom Software

503-922-0135
1000 SW Broadway
Portland, OR

Related Local Events
WOOD TECHNOLOGY CLINIC & SHOW 2010
Dates: 3/9/2010 - 3/11/2010
Location: Oregon Convention Center
Portland, OR
View Details

Party in the Pinot
Dates: 7/25/2009 - 7/25/2009
Location: Oswego Hills Winery
West Linn, OR
View Details