Impementing "Base" pages.

W

Wade

Hi all,

We have created some "Base" class pages for our WebForms and UserControls.
For instance, when we create a WebForm called "WebForm1.aspx", instead of
inheriting from "System.Web.UI.Page" we implement from our "Base" class page
which itself inherits from "System.Web.UI.Page" -- I know, pretty standard.
We do the same with our UserControls, instead they inherit from
"System.Web.UI.UserControl".

Now, there are some methods that we want to include in these "Base" class
pages -- we don't want them in a seperate class because we don't want to
have to make them Shared (Static), nor do we want to instantiate them each
time. However, currently we have to declare these in both our WebForm base
class and our UserControl base class.

Does anyone have any a good way to centralize these methods so that we don't
have to duplicate them, yet they are accessible in each base class page?

Additionally, we have a number of Property's declared in these pages which
get and set data in the session object. These are also currently
duplicated, and we'd like to store them somewhere central so that they are
accessible by both sets of base classes.

Hope this makes sense.

Thanks!

Wade
 
T

Teemu Keiski

Hi,

is it too ugly solution if you'd just provide a typed read-only Page
property (returning type of your base Page class) in your base user
control? That way, derived user controls could access the Page instance in
typed manner, and access the methods etc declared in base page class via
this typed property without the need to re-implement them,

The ugliness there is that it couples your UCs tightly to that specific base
page class. You are able to loosen it a bit,. if you separate these methods
to an interface (UC's typed property would then be of this interface type,
which would be required for page classes to implement), but that can also be
useless if the base page class is used throughout the entire project.
 
T

Teemu Keiski

Assuming your base class for Pages is

BasePage
=======

public class MyBasePage : System.Web.UI.Page
{
public void SomeMethodToAccess()
{
Response.Write("SomeMethodToAccess() called...");
}
}

E.g this is the class serving as base class for your aspx pages (e.g maybe
as a base for the code-behind classes to serve general purposes)

Then the UC base class:

BAseUserControl
=============
public class BaseUserControl : System.Web.UI.UserControl
{
protected MyBasePage BasePage
{
get
{
return this.Page as MyBasePage;
}
}
}

Now your user control's can inherit from this and access the Page in typed
manner for example:

public partial class WebUserControl : BaseUserControl
{
protected void Page_Load(object sender, EventArgs e)
{
this.BasePage.SomeMethodToAccess();
}
}


--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top