Differant pages, same code behind?

B

Beat

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
 
C

Chris Jackson

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

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

General Protection Fault

Beat said:
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.
 
C

Chris Jackson

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
 
S

Srinivas

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.
 

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

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top