473,387 Members | 1,536 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,387 software developers and data experts.

Page_Load event firing twice

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
4 3926
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: H Branyan | last post by:
I have a user control and its Page_Load event is firing twice. I have consulted articles such as this one: http://www.extremeexperts.com/Net/FAQ/PageLoadFiringTwice.aspx This article...
7
by: Shane Bishop | last post by:
I've been fighting with the Page_Load event firing twice. I looked through this user group and saw several other people having similar problems. There were various reasons for it:...
0
by: Glenn | last post by:
Hi I've got a simple page with page load logic associated with it. It is developed in VS NET 2003 Framework 1.1 on Windows 2000 Server This event always fires twice when the page is loading. ...
2
by: Eric Maia | last post by:
I have a textbox (StartDateTextBox) in a UserControl on my page, that is supposed to have a date entered into it. I have a RequiredFieldValidator that has its ControlToValidate property set to the...
4
by: Ed | last post by:
Has anyone seen this one? I have a situation where code in my page_load event is firing twice for mozilla firefox users. Everything is fine in IE6. I set breakpoints and verified. The second time...
14
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) {...
1
by: puja | last post by:
hi all, I have this .aspx page for which the Page_load event occurs twice. I found out while debugging. After searching google, I tried checking with Page.Ispostback method and also had...
1
by: =?Utf-8?B?TWlrZXkgQmFieQ==?= | last post by:
Greetings Hopefully, I can be clear and concise on this one, but I'm confused. I have a page with a ListBox <- ODS <- BusinessObject and a button. The Parameter Source is 'None'. The Default...
4
by: David C | last post by:
I spent the last four hours trying to figure out why Page_Load would execute twice. Even stranger was that everything within if (! IsPostBack){....} executed twice as well. There is no rhyme or...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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,...

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.