using common code for aspx and ascx classes

Discussion in 'ASP .Net' started by Vladimír Kolesnik, Jul 21, 2004.

  1. 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.
     
    Vladimír Kolesnik, Jul 21, 2004
    #1
    1. Advertising

  2. 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.

    "Vladimír Kolesnik" <> wrote in message
    news:...
    > 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.
    >
    >
    >
     
    Kevin Spencer, Jul 21, 2004
    #2
    1. Advertising

  3. 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

    "Kevin Spencer" <> wrote in message
    news:...
    > 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.
    >
    > "Vladimír Kolesnik" <> wrote in message
    > news:...
    > > 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.
    > >
    > >
    > >

    >
    >
     
    Eliyahu Goldin, Jul 22, 2004
    #3
  4. 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.



    "Kevin Spencer" <> pí¹e v diskusním pøíspìvku
    news:...
    > 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.
    >
    > "Vladimír Kolesnik" <> wrote in message
    > news:...
    > > 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.
    > >
    > >
    > >

    >
    >
     
    Vladimír Kolesnik, Jul 22, 2004
    #4
  5. 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.

    "Vladimír Kolesnik" <> wrote in message
    news:#...
    > 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.
    >
    >
    >
    > "Kevin Spencer" <> pí¹e v diskusním pøíspìvku
    > news:...
    > > 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.
    > >
    > > "Vladimír Kolesnik" <> wrote in message
    > > news:...
    > > > 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.
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Kevin Spencer, Jul 22, 2004
    #5
  6. Thanks a lot. Your advice was exactly what I need.



    "Kevin Spencer" <> pí¹e v diskusním pøíspìvku
    news:...
    > 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.
    >
    > "Vladimír Kolesnik" <> wrote in message
    > news:#...
    > > 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.
    > >
    > >
    > >
    > > "Kevin Spencer" <> pí¹e v diskusním pøíspìvku
    > > news:...
    > > > 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.
    > > >
    > > > "Vladimír Kolesnik" <> wrote in message
    > > > news:...
    > > > > 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.
    > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Vladimír Kolesnik, Jul 22, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Quentin
    Replies:
    1
    Views:
    489
    Quentin
    Jul 29, 2003
  2. Jason Shohet
    Replies:
    1
    Views:
    484
    Kevin Spencer
    Nov 10, 2003
  3. Vladimír Karásek

    using common code in ascx and aspx classes

    Vladimír Karásek, Jul 21, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,682
    Bryant Hankins
    Jul 21, 2004
  4. Garth17
    Replies:
    3
    Views:
    3,032
    Brock Allen
    Mar 18, 2005
  5. Replies:
    2
    Views:
    539
Loading...

Share This Page