473,805 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

2.0: Page_Load Event in Base Class?

I've created a base class that inherits from System.Web.UI.P age.
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(objec t 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/
Nov 19 '05 #1
2 1373
On Sun, 18 Sep 2005 15:50:54 -0500, "clintonG"
<cs*********@RE MOVETHISTEXTmet romilwaukee.com > wrote:

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
(AutoEventWireu p=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(Pa ge_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.

--
Scott
http://www.OdeToCode.com/blogs/scott/
Nov 19 '05 #2
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

"Scott Allen" <sc***@nospam.o detocode.com> wrote in message
news:8v******** *************** *********@4ax.c om...
On Sun, 18 Sep 2005 15:50:54 -0500, "clintonG"
<cs*********@RE MOVETHISTEXTmet romilwaukee.com > wrote:

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
(AutoEventWireu p=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(Pa ge_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.

--
Scott
http://www.OdeToCode.com/blogs/scott/

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
2586
by: z. f. | last post by:
Hi, i have a class that is derived from System.Web.UI.Page, and this is the class i use in my application as PageBase. all other page classes are deriverd from my PageBase instead of the original System.Web.UI.Page in order to have common checks in the page base. i make securirty checks in the page base page_load event. if the security fails, i can do whatever i want before the "real" / derived page gets to be executed.
14
13116
by: V. Jenks | last post by:
I'm a little rusty having not touched .NET for 6 months and I can't remember why Page_Load is happening twice in this code: private void Page_Load(object sender, System.EventArgs e) { //existing session? if (Session == null) { //save local empty order object this._newOrder = Orders.Initialize();
2
2229
by: Dave | last post by:
Hi, this is a follow-up to an earlier question but I really haven't found a definitive answer in my search If I have a Base and Derived webform, I've found that the dervived Page_Load event fires first, then the base Page_Load. Is the only technique to change this sequence is to override the Base.Page_Load and explicitely call the base event when you want to as below: Derived WebForm
5
1401
by: Alex | last post by:
We have a web site which we need to implement auditing. I've implemented a base form which all code behind files inherit from, and I can add the logic to it, but that would mean that all existing pages would need to be changed, and any future pages would need to also implement the call code. Is there a way to automatically call some code when a page_load has been invoked without adding code into the Page_Load? Global.asa would be nice,...
5
3167
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits CommonPageBase - Contains myPage which inherits PageBase Each of these classes overrides OnInit and ties an event handler
0
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10369
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3847
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.