472,791 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,791 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 3854
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.