posted 3/12/2007 12:53:33 AM by Vivek Thakur
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 { ...}
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...}
// <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.
Vivek Thakur (Member since: 11/27/2008 11:54:25 AM) I am one of the administrators at CodeAsp.Net and I love programming, architecting solutions, code reviews, teaching and writing about ASP.NET.
View Vivek Thakur 's profile
Leave a comment
It's fast, easy and free! Submit articles, get your own blog, ask questions & give answers in the forums, and become a better developer, faster.
enter your email address: