472,325 Members | 2,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,325 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 2095
> 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...
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...
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...
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...
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...
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...
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...
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...
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...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.