Differant pages, same code behind?

Discussion in 'ASP .Net' started by Beat, Dec 1, 2003.

  1. Beat

    Beat Guest

    Is it possible to reference the same codebehind from 2
    different pages?

    It would be an elegant solution, where we do need
    different settings at the PAGE-Level for 2 user-Groups but
    the rest of the page is the same.
    Page A:
    ....Codebehind="pagea.aspx.vb" validateRequest="True" ...

    Page B:
    ....Codebehind="pagea.aspx.vb" validateRequest="True" ...

    I experienced problems in production that could relate to
    this. And Visual Studio gets a little bit confused...

    your help is very much appreciated
    Beat
    Beat, Dec 1, 2003
    #1
    1. Advertising

  2. Beat

    evolve Guest

    why not do it as a user control (an ascx)?
    if you need to handle events and so on
    see the recent article on www.asp.net


    "Beat" <> wrote in message
    news:a62701c3b820$2d584650$...
    > Is it possible to reference the same codebehind from 2
    > different pages?
    >
    > It would be an elegant solution, where we do need
    > different settings at the PAGE-Level for 2 user-Groups but
    > the rest of the page is the same.
    > Page A:
    > ...Codebehind="pagea.aspx.vb" validateRequest="True" ...
    >
    > Page B:
    > ...Codebehind="pagea.aspx.vb" validateRequest="True" ...
    >
    > I experienced problems in production that could relate to
    > this. And Visual Studio gets a little bit confused...
    >
    > your help is very much appreciated
    > Beat
    evolve, Dec 1, 2003
    #2
    1. Advertising

  3. A better solution would be to use inheritance. Just develop a single class
    deriving from System.Web.UI.Page for your common solution, providing the
    functionality you need, and then have your codebehind classes derive from
    this common class, overriding methods where appropriate.

    For example:

    public class CommonPage : System.Web.UI.Page {
    // common methods and fields here
    }

    public class PageA : CommonPage {
    // inherits common methods and fields
    // overrides as appropriate
    }

    public class PageB : CommonPage {
    // inherits common methods and fields
    // overrides as appropriate
    }

    Obviously, you will want to have any common fields protected, and you will
    be creating this common functionality as a cs file without the benefits of a
    designer, but it is a more object-oriented way of getting at what you want.


    --
    Chris Jackson
    Software Engineer
    Microsoft MVP - Windows Client
    Windows XP Associate Expert
    --
    More people read the newsgroups than read my email.
    Reply to the newsgroup for a faster response.
    (Control-G using Outlook Express)
    --

    "Beat" <> wrote in message
    news:a62701c3b820$2d584650$...
    > Is it possible to reference the same codebehind from 2
    > different pages?
    >
    > It would be an elegant solution, where we do need
    > different settings at the PAGE-Level for 2 user-Groups but
    > the rest of the page is the same.
    > Page A:
    > ...Codebehind="pagea.aspx.vb" validateRequest="True" ...
    >
    > Page B:
    > ...Codebehind="pagea.aspx.vb" validateRequest="True" ...
    >
    > I experienced problems in production that could relate to
    > this. And Visual Studio gets a little bit confused...
    >
    > your help is very much appreciated
    > Beat
    Chris Jackson, Dec 1, 2003
    #3
  4. Beat

    Guest Guest

    Many thanks, Chris,

    I am using VB - and have to say that I do not well
    understand these things...

    The Statment
    >public class CommonPage : System.Web.UI.Page {
    > // common methods and fields here
    >}
    >


    creates an Error. What do I have to write in VB?
    And how do I pass the caller context to this class when
    calling a routine in there?

    I like the concept of a new class where I can put all my
    routines and have the code behind almost empty. But I had
    always problems to reference the Session, Application,
    request objects.






    >-----Original Message-----
    >A better solution would be to use inheritance. Just

    develop a single class
    >deriving from System.Web.UI.Page for your common

    solution, providing the
    >functionality you need, and then have your codebehind

    classes derive from
    >this common class, overriding methods where appropriate.
    >
    >For example:
    >
    >public class CommonPage : System.Web.UI.Page {
    > // common methods and fields here
    >}
    >
    >public class PageA : CommonPage {
    > // inherits common methods and fields
    > // overrides as appropriate
    >}
    >
    >public class PageB : CommonPage {
    > // inherits common methods and fields
    > // overrides as appropriate
    >}
    >
    >Obviously, you will want to have any common fields

    protected, and you will
    >be creating this common functionality as a cs file

    without the benefits of a
    >designer, but it is a more object-oriented way of getting

    at what you want.
    >
    >
    >--
    >Chris Jackson
    >Software Engineer
    >Microsoft MVP - Windows Client
    >Windows XP Associate Expert
    >--
    >More people read the newsgroups than read my email.
    >Reply to the newsgroup for a faster response.
    >(Control-G using Outlook Express)
    >--
    >
    >"Beat" <> wrote in message
    >news:a62701c3b820$2d584650$...
    >> Is it possible to reference the same codebehind from 2
    >> different pages?
    >>
    >> It would be an elegant solution, where we do need
    >> different settings at the PAGE-Level for 2 user-Groups

    but
    >> the rest of the page is the same.
    >> Page A:
    >> ...Codebehind="pagea.aspx.vb" validateRequest="True" ...
    >>
    >> Page B:
    >> ...Codebehind="pagea.aspx.vb" validateRequest="True" ...
    >>
    >> I experienced problems in production that could relate

    to
    >> this. And Visual Studio gets a little bit confused...
    >>
    >> your help is very much appreciated
    >> Beat

    >
    >
    >.
    >
    Guest, Dec 1, 2003
    #4
  5. Beat wrote:

    > Is it possible to reference the same codebehind from 2
    > different pages?
    >
    > It would be an elegant solution, where we do need
    > different settings at the PAGE-Level for 2 user-Groups but
    > the rest of the page is the same.
    > Page A:
    > ...Codebehind="pagea.aspx.vb" validateRequest="True" ...
    >
    > Page B:
    > ...Codebehind="pagea.aspx.vb" validateRequest="True" ...
    >
    > I experienced problems in production that could relate to
    > this. And Visual Studio gets a little bit confused...
    >
    > your help is very much appreciated
    > Beat


    The CodeBehind attribute isn't an actual ASP.NET attribute. It just
    lets VS.NET know which file is the CodeBehind file. You want to change
    the Inherits attribute.

    I don't know if this strategy is a good one, though. Try deriving both
    your pages from a page with common functionality.
    General Protection Fault, Dec 1, 2003
    #5
  6. I don't speak VB particularly well, but basically you end up with something
    like:

    Class CommonPage Inherits System.Web.UI.Page
    ' implementation here
    End Class

    Class PageA Inherits CommonPage
    Protected Overrides Function MyFunction(...) As ...
    ' Implementation of the function you are overriding
    End Function
    End Class

    Class PageB Inherits CommonPage
    ' Any overrides you want
    End Class


    --
    Chris Jackson
    Software Engineer
    Microsoft MVP - Windows Client
    Windows XP Associate Expert
    --
    More people read the newsgroups than read my email.
    Reply to the newsgroup for a faster response.
    (Control-G using Outlook Express)
    --

    <> wrote in message
    news:011701c3b828$9d5e9550$...
    > Many thanks, Chris,
    >
    > I am using VB - and have to say that I do not well
    > understand these things...
    >
    > The Statment
    > >public class CommonPage : System.Web.UI.Page {
    > > // common methods and fields here
    > >}
    > >

    >
    > creates an Error. What do I have to write in VB?
    > And how do I pass the caller context to this class when
    > calling a routine in there?
    >
    > I like the concept of a new class where I can put all my
    > routines and have the code behind almost empty. But I had
    > always problems to reference the Session, Application,
    > request objects.
    >
    >
    >
    >
    >
    >
    > >-----Original Message-----
    > >A better solution would be to use inheritance. Just

    > develop a single class
    > >deriving from System.Web.UI.Page for your common

    > solution, providing the
    > >functionality you need, and then have your codebehind

    > classes derive from
    > >this common class, overriding methods where appropriate.
    > >
    > >For example:
    > >
    > >public class CommonPage : System.Web.UI.Page {
    > > // common methods and fields here
    > >}
    > >
    > >public class PageA : CommonPage {
    > > // inherits common methods and fields
    > > // overrides as appropriate
    > >}
    > >
    > >public class PageB : CommonPage {
    > > // inherits common methods and fields
    > > // overrides as appropriate
    > >}
    > >
    > >Obviously, you will want to have any common fields

    > protected, and you will
    > >be creating this common functionality as a cs file

    > without the benefits of a
    > >designer, but it is a more object-oriented way of getting

    > at what you want.
    > >
    > >
    > >--
    > >Chris Jackson
    > >Software Engineer
    > >Microsoft MVP - Windows Client
    > >Windows XP Associate Expert
    > >--
    > >More people read the newsgroups than read my email.
    > >Reply to the newsgroup for a faster response.
    > >(Control-G using Outlook Express)
    > >--
    > >
    > >"Beat" <> wrote in message
    > >news:a62701c3b820$2d584650$...
    > >> Is it possible to reference the same codebehind from 2
    > >> different pages?
    > >>
    > >> It would be an elegant solution, where we do need
    > >> different settings at the PAGE-Level for 2 user-Groups

    > but
    > >> the rest of the page is the same.
    > >> Page A:
    > >> ...Codebehind="pagea.aspx.vb" validateRequest="True" ...
    > >>
    > >> Page B:
    > >> ...Codebehind="pagea.aspx.vb" validateRequest="True" ...
    > >>
    > >> I experienced problems in production that could relate

    > to
    > >> this. And Visual Studio gets a little bit confused...
    > >>
    > >> your help is very much appreciated
    > >> Beat

    > >
    > >
    > >.
    > >
    Chris Jackson, Dec 1, 2003
    #6
  7. Beat

    Srinivas Guest

    Hi

    If I write an abstract base class that extends System.Web.UI.Page and
    have individual code-behind classes derive from this base class,
    VS.Net throws an error when I try to open the Design UI of the web
    page.

    Is there a work-around for this? The reason to have the base class as
    abstract is to enforce implementation of certain methods, say page
    initialization, on each derived page.

    - Srinivas.

    General Protection Fault <> wrote in message news:<>...
    > Beat wrote:
    >
    > > Is it possible to reference the same codebehind from 2
    > > different pages?
    > >
    > > It would be an elegant solution, where we do need
    > > different settings at the PAGE-Level for 2 user-Groups but
    > > the rest of the page is the same.
    > > Page A:
    > > ...Codebehind="pagea.aspx.vb" validateRequest="True" ...
    > >
    > > Page B:
    > > ...Codebehind="pagea.aspx.vb" validateRequest="True" ...
    > >
    > > I experienced problems in production that could relate to
    > > this. And Visual Studio gets a little bit confused...
    > >
    > > your help is very much appreciated
    > > Beat

    >
    > The CodeBehind attribute isn't an actual ASP.NET attribute. It just
    > lets VS.NET know which file is the CodeBehind file. You want to change
    > the Inherits attribute.
    >
    > I don't know if this strategy is a good one, though. Try deriving both
    > your pages from a page with common functionality.
    Srinivas, Dec 2, 2003
    #7
    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. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    583
    Alphonse Giambrone
    Jun 28, 2003
  2. Peri
    Replies:
    2
    Views:
    2,954
  3. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    482
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 19, 2007
  4. dan
    Replies:
    1
    Views:
    1,063
    bruce barker
    Mar 22, 2008
  5. WebBuilder451
    Replies:
    2
    Views:
    598
    clintonG
    Jun 27, 2008
Loading...

Share This Page