473,725 Members | 2,409 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.EventHan dler(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.EventHan dler(this.MyPag e_Load);

and also it's respective event handler to:

private void MyPage_Load(obj ect sender, System.EventArg s 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_Lo ad).

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(th is.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 3987
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.goo glegroups.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.EventHan dler(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.EventHan dler(this.MyPag e_Load);

and also it's respective event handler to:

private void MyPage_Load(obj ect sender, System.EventArg s 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_Lo ad).

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(th is.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.goo glegroups.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
1176
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 essentially says that if I set AutoEventWireUp to false, then the Page_Load will only fire once. However, that is not the right solution for me. Here is my code: in the .ascx.cs file:
7
2344
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: AutoEventWireup="true" instead of AutoEventWireup="false" Spyware software And having your events all wired wrong I had done everything to my machine, stripped it down to just have ..NET on it and I was still having this problem until...
0
281
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. I made sure that AutoWireUp is false, and that "this.Load += new System.EventHandler(this.Page_Load);" is in InitializeComponent() Is this a known problem? If so is there a known workaround Thanks
2
3194
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 textbox. I also have a CompareValidator pointed to the textbox with its Type="Date." Despite all documentation to the contrary, I am getting an exception when the page first loads that is thrown by the CompareValidator, complaining that it...
4
1712
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 the page_load fires in mozilla, I checked the IsPostback to verify that it is still false. What causes this behavior? Thanks, Ed
14
13113
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) { //existing session? if (Session == null) { //save local empty order object this._newOrder = Orders.Initialize();
1
6954
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 "AutoEventWireUp" = false and also run after removing this tag from page directive but still my page_load event gets called twice. I have 3 pages called Search.asp, Processing.aspx and ConfirmSearch.aspx.
1
1839
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 Value is left blank. Both are set in the Page_Load (myODS.SelectParameters.DefaultValue = 999) I put Debug statements in the Page_Load, ODS_Load and method of my
4
2859
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 reason for that. Here is what I found out. When you have an ImageButton with no ImageURL set, it will always do that. This was not the case with .NET 1.0. So if you need to experiment with ImageButton, set something to the ImageUrl
0
8748
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9248
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8072
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6695
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6000
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4506
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3212
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2151
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.