using common code for aspx and ascx classes

  • Thread starter Thread starter Vladimír Kolesnik
  • Start date Start date
V

Vladimír Kolesnik

Hi there,
does anybody know, how to create a class, which is can be inherited by both
aspx (System.Web.UI.Page) as well as by ascx (System.Web.UI.UserControl)
classes.
I have the bunch of same methods, which I want to reuse in both ascx and
aspx classes. Any idea how to do it?
Thanks for all your hints.
 
Create a class that inherits System.Web.UI.Page, or
System.Web.UI.UserControl, add whatever code to it that you need to be
global to all Pages/UserControls, and inherit that class in your
Pages/UserControls.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Good suggestions. Alternatively, if for whatever reason it doesn't suit you,
you can make just a class library with static functions that will get the
reference to the control as a parameter.

Eliyahu
 
The solution you described I already knew, thanks anyway. Maybe I was not
clear enough describing the problem, I have class MyWebPage derived from
System.Web.UI.Page (aspx), where I have often used methods for example for
dealing with database connection etc. All aspx pages I derive from
MyWebPage, everything is fine. The problem is when I have ascx class
derived from System.Web.UI.UserControl and I want to use methods to get
connection to the database and some other methods that are placed in the
MyWebPage class. Do I have to make a class similar as MyWebPage just
inherited from System.Web.UI.UserControl, for example MyUserControl to be
able use these methods for all ascx classes? I think there should be a way
to pass the problem having same methods defined in two classes.
 
Well, you can't inherit from more than one base class, so that's not a
possibility. You could certainly implement an Interface, which would require
both classes to implement the same properties/methods defined in the
Interace. You could create a class which has all the shared functionality,
and include that class as a property of both base classes.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Back
Top