I have found using a Base Page class in my web projects immensely useful. What exactly is a Base Page class?
We know that all our web page code behinds derive from System.Web.UI.Page class as:
public
partial class Default4 : System.Web.UI.Page
{ ...}
Now, many times we need to code the same thing in every page class, for example consider these scenarios:
1. Setting the culture of the current thread in the Page class's InitilaizeCulture() method so that all pages belong to the same user selected culture.
2. Redirecting the user to some “time out“ web page when the user Session has expired.
3. Getting the value of some control (like search textbox) which is in the MasterPage.
4. Setting the Meta keywords and description tags for search engine optimization.
5. Any other code which is common across most pages.
We can make our life easier by creating a base class and deriving all our pages from this class instead of System.Web.UI.Page class as:
//
<summary>
/// BasePage for the common funtionality in all
/// the web pages of the site.
/// </summary>
public class BasePage: System.Web.UI.Page
{
//common code...
}
Now all our other pages will derive from this class:
public
partial class _Default : BasePage
{
.///Code...
}
This will make our code more maintainable.