By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

Impementing "Base" pages.

P: n/a
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
Jan 21 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

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

"Wade" <wwegner23NOEMAILhotmail.com> wrote in message
news:Oo*************@TK2MSFTNGP12.phx.gbl...
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

Jan 21 '06 #2

P: n/a
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

"Teemu Keiski" <jo****@aspalliance.com> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
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.

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

"Wade" <wwegner23NOEMAILhotmail.com> wrote in message
news:Oo*************@TK2MSFTNGP12.phx.gbl...
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


Jan 26 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.