2.0: Page_Load Event in Base Class?

C

clintonG

I've created a base class that inherits from System.Web.UI.Page.
I've been using Page_PreInit in the base class to change Themes.

I'm using MasterPages and need a bunch of code in each content page to
manage the UI. For the most part this UI code is the same across many pages
although their will be some conditional circunstances. So I thought "put the
logic in the base class so I don't even have to call any methods in each
content page."

I thought partial classes would also allow me to use a Page_Load event in
the base class but I can't even get a Response.Write to return results.

Noting I've tried several types of accessors in the signature...

protected void Page_Load(object sender, EventArgs e)
{ ... }

How can I use a Page_Load event on the base class when using 2.0?

Secondly, might it be a better approach to create my own classes and
App_Code and call methods from each content page anyway?

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/
 
S

Scott Allen

How can I use a Page_Load event on the base class when using 2.0?

It works just like 1.x, except there is one trick now.

In 2.0, Page events are magically attached to event handlers
(AutoEventWireup=true) using reflection. This only happens for the
most derived type.

If you want a base class with a load event, you'll have to explicitly
wire it up, i.e. your base class could use:

this.Load += new EventHandler(Page_Load);

in the constructor.

I'd use an approach like this if the events are independent of each
other. If the Page_Load in your derived class requires Page_Load in
the base class to fire first, then there is a dependency between the
two that might be broken. In that case I'd use your other approach of
having a helper class in App_Code or base class library to call into.
 
C

clintonG

Thanks for commenting Scott. I posted this to the 2.0 Forums and another
reply suggested over-riding OnLoad in the base class.
I'm going to study both approaches as I've no experience with either and its
time I learned to do so.

BTW -- if you would recall an earlier RFI I posted about controls that
disappeared from the Master? I resolved the problem by creating properties
that I could access from the content page(s) allowing me to modify
properties of the controls to maintain their visibility. All of the code to
do that in each content page is what led me to the topic we are now
discussing which can be summarized as 'maintaining control state' I suppose.

<%= Clinton Gallagher
 

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

Top