Page Load called twice!

  • Thread starter Thread starter Colin Basterfield
  • Start date Start date
C

Colin Basterfield

Hi,

Hmmm, a strange one for me, but hopefully not for others...

I have a base page which has

virtual protected void PageLoadEvent(object sender, System.EventArgs e){}

In the Page_Load method I call this

PageLoadEvent(sender, e);

In all the forms that inherit from it I have a protected override of
PageLoadEvent method which does the necessary for that particular form,
nothing too complex there, what is strange however is that one of the forms
which is a summary page has a datagrid which has hyperlinks in, and if I
click on one of these links it goes to a 'info breakdown page' driven by a
parameter. Although there is no Page_Load method in the 'info breakdown
page' as it is replaced by the PageLoadEvent method, this method gets called
once independently, and then again through the base page Page_Load event
method.

The way I proved this was to put a breakpoint in both the Page_Load event of
the Base Page, and one in the 'info breakdown page' PageLoadEvent method.
So then I click the hyperlink on the summary page, and it immediately stops
in PageLoadEvent on the 'info breakdown page', and then if you F5 it, it
stops in the base page Page_Load method code, which of course calls the
PageLoadEvent of the 'info breakdown page'.

This isn't the case for the other pages, and there only difference is that
the 'info breakdown page' is accessed/initiated by a hyperlink.

Any ideas anyone?
cheers in advance
Colin B
 
Hi Colin,

Check the setting of AutoEventHookup on the page. If you're using CodeBehind
pages you generally don't want this as the CodeBehind page automatically
generates the OnLoad() event hookup.

Also if your form is inherited and your event hookups in the base class (in
InitializeComponent) you will end up hooking the eventhandler multiple times
and so it fires more than once. The solution there is to remove the OnLoad
code manually in the implementation level of the form.

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/webblog/
 
Hi Rick,

It took a few minutes for the penny to drop reading your reply, and now I
see what you mean, I don't need this.Load setting to anything in the forms
that inherit the base because that will call the PageLoadEvent method. In
my child forms I had that set to:

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

which of course would cause it to be called twice!

I took this line out, and voila, it only gets called once. The reason I
didn't spot it was that it appeared like it was only doing it for the form
that was initiated from the hyperlink because that was the only page that
generated dynamic controls, whereas the others only filled some labels up
text, so it only overwrote the second time around!

uh duh, I should have known that!
Thanks for your help!
Cheers
Colin
 
Back
Top