473,414 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Inheritance & Page_Load (again)

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
--------------------
override private void Page_Load(object sender, System.EventArgs e)
{
base.Page_Load(sender, e); //Run it first
'--Then the rest of dervived code goes here...
}

To me, if you were building a framework and had some generic code in the
base class, that should run first before the derived more specific code.

In the case above, if you wanted the base code to run first, all your
developers would then have to explicitly remember to override and call
base.Page_Load.

If this is the default behavior of asp.net that fine, I just wanted to find
a definate answer.

Thanks, Dave.
Nov 19 '05 #1
2 2175
> To me, if you were building a framework and had some generic code in the
base class, that should run first before the derived more specific code.
To me, you should have a choice about it. Sometimes it needs to run in one
order, and sometimes another. Who cares what the default is? What matters is
that you (the developer) knows which order it needs to run in, and codes it
appropriately. And you seem to know that. So, relax and have a cup of
Starbucks!

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.

"Dave" <Da**@discussions.microsoft.com> wrote in message
news:EF**********************************@microsof t.com... 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
--------------------
override private void Page_Load(object sender, System.EventArgs e)
{
base.Page_Load(sender, e); //Run it first
'--Then the rest of dervived code goes here...
}

To me, if you were building a framework and had some generic code in the
base class, that should run first before the derived more specific code.

In the case above, if you wanted the base code to run first, all your
developers would then have to explicitly remember to override and call
base.Page_Load.

If this is the default behavior of asp.net that fine, I just wanted to
find
a definate answer.

Thanks, Dave.

Nov 19 '05 #2
Hi Dave:

This is the default behavior, but let's clarify what's going on.

I assume you are wiring up the event during OnInit in the base class.
Event handlers are invoked in the order in which they were added.
So, if you take a look at the webform designer generated code:

override protected void OnInit(EventArgs e)
{
// CODEGEN: This call is required ...
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}

OnInit, being virtual, will be invoked on the most derived type. In
this case the derived type invokes InitializeComponent and subscribes
to the Load event first. Next, the derived type invokes
base.OnInit(e), where your base class will subscribe to the Load event
in second place.

One solution is to override OnLoad in your base class, perform some
work, and then invoke base.OnLoad. You also might want to review the
design to see why you depend on the order of events firing - it might
be better not to depend on a specific behavior.

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

On Fri, 18 Mar 2005 08:31:10 -0800, "Dave"
<Da**@discussions.microsoft.com> wrote:
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
--------------------
override private void Page_Load(object sender, System.EventArgs e)
{
base.Page_Load(sender, e); //Run it first
'--Then the rest of dervived code goes here...
}

To me, if you were building a framework and had some generic code in the
base class, that should run first before the derived more specific code.

In the case above, if you wanted the base code to run first, all your
developers would then have to explicitly remember to override and call
base.Page_Load.

If this is the default behavior of asp.net that fine, I just wanted to find
a definate answer.

Thanks, Dave.


Nov 19 '05 #3

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

Similar topics

8
by: Shawn Casey | last post by:
Consider the following code: interface IBase { virtual void BaseFunction() = 0; }; interface IDerived : public IBase { virtual void DerivedFunction() = 0;
11
by: Josh Lessard | last post by:
Hi all. I'm maintaining a C++ program and I've come across a nasty piece of code that works, but I just don't understand why. I'm not actually this part of the program, but I really want to know...
0
by: Alice Bag | last post by:
groovy feature request: I think comments on COLUMNs should be inherited from the "parent" table when the "child" table is created. If you make use of inheritance, this would be a big time...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
2
by: JR | last post by:
I'm developing in VS.NET 2002 and I'm having trouble working around the issue with page inheritance and the VS.NET Form Designer. When I create a base class for my form, and inherit from it, I...
2
by: Emmanuel | last post by:
Hi, I'm working on a c# web app and require having some code which runs in the page Load event of each page and to be reusable in other web apps. So i decided to use a Class Library which...
1
by: dirk van waes | last post by:
Hello everyone, Being complete newbie in asp.net I am trying to make an example which works with a very simple database. First I made my project in VS- vb.net, draging an oledbconnection and an...
17
by: Arpan | last post by:
When a Button is clicked in a Web Form in an ASPX page, the Form will post back to itself. Under such circumstances (i.e. when a Button is clicked), will the Page_Load sub execute first & then will...
42
by: coder_lol | last post by:
Thanks everyone again for contributing to helping me clear C++ confusions. I did some serious reading on copy constructors and assignments and I think I've got a good handle on the memory stuff. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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,...
0
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...

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.