Anyone who is new to the world of ASP .NET, or new to the concepts of inheritence in general probably is missing out on a great way to organize their ASP .NET application. When you first create a webform and switch to code view you are faced with the following statement (c#):
public class YourPage : System.Web.UI.Page
What many new coders do not realize is that they can create class files that inherit System.Web.UI.Page and embed common functionality into those pages. Shown here is a sample of a new class BasePage that inherits System.Web.UI.Page. Notice that it exposes a property called connectstring which would allow any pages that inherit BasePage to already have a lookup for the connection string.
public class BasePage : System.Web.UI.Page
{ private string _connectstring = "";
protected string connectstring
{ get
{ if(_connectstring == "")
{ //Get Connection string
}
return _connectstring;
}
}
public BasePage()
{ //
// TODO: Add constructor logic here
//
}
}
All you have to do to inherit the BasePage is to change:
public class YourPage : System.Web.UI.Page To public class YourPage : BasePage
A common question I get from new developers when I present this concept to them is “How can I tell when something should be moved to a base page?”. For me personally, if a segment of code is used more than twice, it goes into a BasePage. Keep in mind that through the power of inheritance you can create a top level BasePage which has functions common to the majority of the application and Inherit the BasePage class into more page specific classes. For example:

If the above represented a theoretical order taking system you could group common order functionality into the OrderPage class, the customer functionality into the CustomerPage class, and the Administrator functions into the AdminPage class. By inheriting one of these classes you can gain access to the special functionality they provide. The end result is code organization that is clean and easy to follow.