The Gotcha
This is a really sore subject for me because I have tried finding a unifying factor for both sets of objects, but the answer I have come up with is – there is no unifying factor. I have read posts of people coming up with ways to have a single base class for both pages and controls – but it was a very over complicated approach and they didn’t post how to do it anyhow.

http://stackoverflow.com/questions/7840706/how-to-make-a-base-class-for-both-page-and-usercontrol

I followed the advice in the above link and I have decided it is the best approach (at least for me) to follow.

The Approach
When creating a new web app from scratch, I highly recommend almost all of your page and control code behinds inheriting from a base class. The problem obviously is when you need to have code available to both base classes to do something rudimentary and basic. I recommend having the following components:

  1. Page Base Class
  2. Control Base Class
  3. Static Class that has extension and static methods in it that both Base Classes will be using. This way you don’t duplicate any code.
  4. Interface for the above classes. It is not fun to maintain, but it guarantees that the implementations and signatures being used are identical for both classes.

The following is an example of the implementation. These are methods I use because they make my life easier for doing redirects and constructing URIs in the code behind. Feel free to use them.

Pages


Controls


Static Class


Interface

Leave a Reply

Your email address will not be published. Required fields are marked *