470,641 Members | 2,453 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,641 developers. It's quick & easy.

Page Load called twice!

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
Nov 18 '05 #1
2 6252
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/
----------------------------------
Making waves on the Web
"Colin Basterfield" <co**************@hotmail.com> wrote in message
news:uh**************@TK2MSFTNGP11.phx.gbl...
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

Nov 18 '05 #2
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

"Rick Strahl [MVP]" <ri********@hotmail.com> wrote in message
news:OP**************@tk2msftngp13.phx.gbl...
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/
----------------------------------
Making waves on the Web
"Colin Basterfield" <co**************@hotmail.com> wrote in message
news:uh**************@TK2MSFTNGP11.phx.gbl...
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


Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by mart | last post: by
7 posts views Thread by Goober | last post: by
3 posts views Thread by Imran Aziz | last post: by
8 posts views Thread by MaryA | last post: by
9 posts views Thread by mosscliffe | last post: by
11 posts views Thread by fiefie.niles | last post: by
1 post views Thread by Korara | last post: by
???
1 post views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.