Page Load called twice!

Discussion in 'ASP .Net' started by Colin Basterfield, Dec 22, 2003.

  1. 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
    Colin Basterfield, Dec 22, 2003
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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
    >
    >
    Rick Strahl [MVP], Dec 22, 2003
    #2
    1. Advertising

  3. 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]" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:...
    > > 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
    > >
    > >

    >
    >
    Colin Basterfield, Dec 22, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    0
    Views:
    1,756
    Andreas Klemt
    Jul 4, 2003
  2. Samy
    Replies:
    2
    Views:
    1,350
  3. =?Utf-8?B?UmFqZWV2?=

    Page Submission Events are called twice

    =?Utf-8?B?UmFqZWV2?=, May 17, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    334
    =?Utf-8?B?UmFqZWV2?=
    May 17, 2006
  4. Kiuhnm

    twice(twice(x))

    Kiuhnm, Apr 1, 2006, in forum: C++
    Replies:
    2
    Views:
    393
    Kiuhnm
    Apr 1, 2006
  5. Moshe
    Replies:
    0
    Views:
    101
    Moshe
    Jul 16, 2003
Loading...

Share This Page