Page_Load Event fires twice....sometimes!

Discussion in 'ASP .Net' started by doobdave, Dec 17, 2004.

  1. doobdave

    doobdave Guest

    Hi all,

    I'm experiencing a rather strange problem:

    I am coding a web application using VB.net ASP.net and Visual Studio
    2003.
    It's quite a large application, but the problem only occurs on one of
    the Web User Controls.

    This particular Web User Control has some text entry fields in it,
    drop-downs etc... and a button which brings up a pop-up form.
    The pop-up form is used to add a new entry into one of the listboxes
    on the 'parent' form. The pop-up is a modal (created using
    window.showmodaldialog), and when it's closed, the 'parent' form is
    resubmitted using javascript, and the listbox repopulated to show the
    newly added item(s).
    All this is working fine.

    However, on some occasions (and I can't work out what differences
    are), when the pop-up form closes, the parent page loads and the page
    events fire in the following sequence:
    - Page_Load
    - Page_Load checks if postback=true, then loads a listbox control with
    the items that need to be in it.
    - Page_Load fires again....

    Now this appears to be happening randomly, and when this happens, the
    listbox does not contain the newly-added entries.
    On some occasions (I am doing exactly the same procedure to add
    items), the Page_Load event does not fire the second time, and the
    listbox contains all the entries that it should.

    This is driving me crazy! I can't understand why it sometimes does it
    and sometimes does not...

    I have searched newsgroups for an answer, and the majority say that
    this problem is caused by setting the AutoEventWireUp page directive
    attribute to true. I have checked, and it is set to false in the
    control and the hosting page.

    If anyone has any ideas and/or suggestion, that would be greatly
    appreciated.

    Thanks in advance,

    David
     
    doobdave, Dec 17, 2004
    #1
    1. Advertising

  2. doobdave

    Vladimir Guest

    David, I experienced such a problemn some time. It was rather big
    application too and I used there coroporate libraries. The trick was that I
    had to inherit the control calss from that library when creating my new one.
    It was very impotrant there to ovverride Page_Load event handler and fire
    base.Page_Load (sender, e) within this handler.
    Check it out.
    Hope it will help.

    --

    Vlad

    "doobdave" <> wrote in message
    news:...
    > Hi all,
    >
    > I'm experiencing a rather strange problem:
    >
    > I am coding a web application using VB.net ASP.net and Visual Studio
    > 2003.
    > It's quite a large application, but the problem only occurs on one of
    > the Web User Controls.
    >
    > This particular Web User Control has some text entry fields in it,
    > drop-downs etc... and a button which brings up a pop-up form.
    > The pop-up form is used to add a new entry into one of the listboxes
    > on the 'parent' form. The pop-up is a modal (created using
    > window.showmodaldialog), and when it's closed, the 'parent' form is
    > resubmitted using javascript, and the listbox repopulated to show the
    > newly added item(s).
    > All this is working fine.
    >
    > However, on some occasions (and I can't work out what differences
    > are), when the pop-up form closes, the parent page loads and the page
    > events fire in the following sequence:
    > - Page_Load
    > - Page_Load checks if postback=true, then loads a listbox control with
    > the items that need to be in it.
    > - Page_Load fires again....
    >
    > Now this appears to be happening randomly, and when this happens, the
    > listbox does not contain the newly-added entries.
    > On some occasions (I am doing exactly the same procedure to add
    > items), the Page_Load event does not fire the second time, and the
    > listbox contains all the entries that it should.
    >
    > This is driving me crazy! I can't understand why it sometimes does it
    > and sometimes does not...
    >
    > I have searched newsgroups for an answer, and the majority say that
    > this problem is caused by setting the AutoEventWireUp page directive
    > attribute to true. I have checked, and it is set to false in the
    > control and the hosting page.
    >
    > If anyone has any ideas and/or suggestion, that would be greatly
    > appreciated.
    >
    > Thanks in advance,
    >
    > David
     
    Vladimir, Dec 17, 2004
    #2
    1. Advertising

  3. doobdave

    Guest

    Hi Vladimir,

    Thanks for replying.

    This problem is really driving me nuts!!
    I can't understand why sometimes the page works fine, and other times
    (even in the same session) the Page_Load fires twice.

    I'm not sure if your suggestion applies in this case, as I have several
    other user controls that use the same template, and none of them
    display this behaviour.
    If you have any more ideas, I would be extremely grateful.
    Regards,
    David
     
    , Dec 17, 2004
    #3
  4. doobdave

    Jeff Evans Guest

    < > wrote:

    > If you have any more ideas, I would be extremely grateful.


    I don't know if this will necessarily help you or not, but I always, ALWAYS,
    get rid of that stupid Page_Load method in .aspx.cs files because if you
    look closely, that is essentially an event handler wired up by the page
    itself in the Windows Form Designer generated region (or whatever it's
    called), I think in InitializeComponent(). This is unneccessary overhead (a
    page listening for its own events). It's much better to just delete this
    line from InitializeComponent:

    this.Load += ...

    and instead override the OnLoad method:

    protected override void OnLoad(EventArgs e)
    {
    base.OnLoad(e);
    //whatever else you were going to do in Page_Load
    }


    I've never had problems with double Page_Loads since using this technique.
     
    Jeff Evans, Dec 17, 2004
    #4
    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. H Branyan

    Page_Load fires twice

    H Branyan, Oct 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    684
    H Branyan
    Oct 22, 2003
  2. Jon
    Replies:
    1
    Views:
    11,393
    George Ter-Saakov
    Jan 13, 2004
  3. Jay Douglas
    Replies:
    7
    Views:
    609
  4. =?Utf-8?B?RWQ=?=

    Page_Load fires twice with Mozilla

    =?Utf-8?B?RWQ=?=, Nov 20, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    4,476
    shailpatel83
    Apr 2, 2010
  5. =?Utf-8?B?RXJpayBKZW5zZW4=?=

    CodeBehind But Not Postback - Page_Load fires twice

    =?Utf-8?B?RXJpayBKZW5zZW4=?=, Jul 19, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    3,324
    =?Utf-8?B?RXJpayBKZW5zZW4=?=
    Jul 21, 2005
Loading...

Share This Page