By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,949 Members | 2,043 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,949 IT Pros & Developers. It's quick & easy.

Page_Load event firing twice

P: n/a
Again, I'm rather new here, so if I fail to follow any etiquette,
please forgive me and let me know what I've done wrong, but I think
this might interest quite a few people.

One of my colleaques was endeavoring to create a custom user control to
make things a bit simpler, but she noticed that her Page_Load
eventhandler was firing twice. So after long hours of research and
experimentation, I stumbled upon, imho, is quite the discovery.

If we look at the cookie-cutter code that VS2003 gives us, it reads:

this.Load += new System.EventHandler(this.Page_Load);

As we can see, we are concatenating or appending another EventHandler
onto the this.Load eventhandler which fires on the OnLoad event of the
page, custom control, whatever may use it.

So if I were to have just looked here, I would have realized that there
is a default event handler there, and obviously we are adding another
to it. That default handler, I assumed, is Page_Load.

So when I changed the cookie-cutter'd line to this:

this.Load += new System.EventHandler(this.MyPage_Load);

and also it's respective event handler to:

private void MyPage_Load(object sender, System.EventArgs e)

I expected to get rid of the double firing. And it worked! The Load
event was not fired twice!

So I suspected that the Page_Load event was only fired when there was a
proper event handler created, as in (this.MyPage_Load).

Not SO! I then, just on a hunch, added a function like so:

private void Page_Load() {}

and added a response.Write() to see if it would fire or not.

And it did!! Absolutely nuts. It looks to me like ASP is purposefully
looking for any function that is named "Page_Load" and fires it!!

And furthermore, my colleague did a bit more research, and even after
this line:

this.Load -= new EventHandler(this.Page_Load);

it STILL fired the function!!

Does anyone have any clarification on this, or is this something rather
new? Either way, it's probably one of the dumbest and most annoying
things I've seen microsoft do yet. -_-
--Seraph

Nov 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
sam
Yes!! All by yourself you've discovered event handlers and the
AutoEventWireup attribute in .NET!! :)
From the documentation on AutoEventWireup:

---
Alternatively, the ASP.NET page framework also supports an automatic
way to associate page events and methods. If the AutoEventWireup
attribute of the Page directive is set to true (or if it is missing,
since by default it is true), the page framework calls page events
automatically, specifically the Page_Init and Page_Load methods. In
that case, no explicit Handles clause or delegate is needed.
---
But I'm still surprised that your Page_Load() gets invoked, since it
doesn't have the necessary arguments. Anyone??

Nov 19 '05 #2

P: n/a
if AutoEventWireup is not set to false (VS usually spews this), then
Page_Load is fired automatically.

-- bruce (sqlwork.com)
"Seraph" <se**************@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Again, I'm rather new here, so if I fail to follow any etiquette,
please forgive me and let me know what I've done wrong, but I think
this might interest quite a few people.

One of my colleaques was endeavoring to create a custom user control to
make things a bit simpler, but she noticed that her Page_Load
eventhandler was firing twice. So after long hours of research and
experimentation, I stumbled upon, imho, is quite the discovery.

If we look at the cookie-cutter code that VS2003 gives us, it reads:

this.Load += new System.EventHandler(this.Page_Load);

As we can see, we are concatenating or appending another EventHandler
onto the this.Load eventhandler which fires on the OnLoad event of the
page, custom control, whatever may use it.

So if I were to have just looked here, I would have realized that there
is a default event handler there, and obviously we are adding another
to it. That default handler, I assumed, is Page_Load.

So when I changed the cookie-cutter'd line to this:

this.Load += new System.EventHandler(this.MyPage_Load);

and also it's respective event handler to:

private void MyPage_Load(object sender, System.EventArgs e)

I expected to get rid of the double firing. And it worked! The Load
event was not fired twice!

So I suspected that the Page_Load event was only fired when there was a
proper event handler created, as in (this.MyPage_Load).

Not SO! I then, just on a hunch, added a function like so:

private void Page_Load() {}

and added a response.Write() to see if it would fire or not.

And it did!! Absolutely nuts. It looks to me like ASP is purposefully
looking for any function that is named "Page_Load" and fires it!!

And furthermore, my colleague did a bit more research, and even after
this line:

this.Load -= new EventHandler(this.Page_Load);

it STILL fired the function!!

Does anyone have any clarification on this, or is this something rather
new? Either way, it's probably one of the dumbest and most annoying
things I've seen microsoft do yet. -_-
--Seraph

Nov 19 '05 #3

P: n/a
Yeah...I'm surprised too. That shouldn't even compile.
private void Page_Load() {} does not have the proper delegate signature. So,
unless he is not telling us all the story..I don't see how that would have
worked.

--
TDAVISJR
aka - Tampa.NET Koder
"sam" <sa*************@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Yes!! All by yourself you've discovered event handlers and the
AutoEventWireup attribute in .NET!! :)
From the documentation on AutoEventWireup:

---
Alternatively, the ASP.NET page framework also supports an automatic
way to associate page events and methods. If the AutoEventWireup
attribute of the Page directive is set to true (or if it is missing,
since by default it is true), the page framework calls page events
automatically, specifically the Page_Init and Page_Load methods. In
that case, no explicit Handles clause or delegate is needed.
---
But I'm still surprised that your Page_Load() gets invoked, since it
doesn't have the necessary arguments. Anyone??

Nov 19 '05 #4

P: n/a
Sam: What, in fact, was 'discovered' was indeed _not_ AutoEventWireup.
It is set to false. Thanks for the sarcasm, though.

Bruce: AutoEventWireup is set to false.

AutoEventWireup is set to false by default in VisualStudio. To think
that I would set this to true and then spend hours experimenting with
it just to post to a newsgroup and ask for advice is absurd.

TDAVISJR: Exactly the reason for the post. Page_Load is not a delegate,
obviously, because it is being fired even without the correct
signature. I would encourage you to try this for yourself if you have
some spare time. You'll see that no matter what function you create, if
it is named Page_Load(), it will fire. Here is the scenario exactly.

Create a new project. In that project, create a new WebUserControl.
Inside the WebUserControl, use your own methods of keeping track of the
Init's and PageLoad's. Add the control to the page dynamically in the
OnInit of the page.

The event will fire twice, unless you change it's name. Then it only
fires once. If you add a function for Page_Load to your WebUserControl,
now that will also fire, _no matter what signature you use_. I'm going
to do some more testing and use a command-line compiler and a simple
text editor to see if it's something going on behind the scenes with
VS. Hopefully it is, otherwise it's something to do with ASP.NET's
engine O.0.

So aside from assuming that I had used AutoEventWireup, does anyone
else have any suggestions?

Nov 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.