none
Page_Load called twice. RRS feed

  • Question

  • Hi,

     

    Does anyone know why a Page_Load would be called twice on a page when requesting an aspx page?

    I am using .Net 2.0 with the new .net 1.1 compilation model installed.

    Whenever I request a page, the Page_Load on the aspx page appears to be called twice, so does the Page_Load on and user controls added to that page.

     

    Monday, July 3, 2006 3:29 PM

Answers

  • Check to see if you are binding the event.
    If you migrated the application from 2k3 then it might be using the old fashioned method of binding the events.

    So if the OnLoad event is getting bound twice that would be about the only other way off the tip of my head that I know of that that an event could get fired twice unless a 2nd request was not being made.
    Monday, July 3, 2006 4:21 PM
    Moderator

All replies

  • Without some source it is difficult to know exactly when and where your problem is occurring. It could be you having the Page_Load function wired up twice, or even you causing a postback without knowing it.

    That being said though you may have better luck in asking your question over on the ASP.NET forums where the folks there tend to think in a more ASP.NET centric sort of way.

    Monday, July 3, 2006 3:34 PM
    Moderator
  • This is most likely happening because you have a tag that references a file that is blank.
    for example <img src="" /> will make a call to the Default.aspx. Usually it's a blank image tag. There is a possibility it could also be a <script or <style or <link tag also.


    Monday, July 3, 2006 3:45 PM
    Moderator
  • Hi,

    This isnt the case as it happens on a test page I built with no html content at all.

    • Proposed as answer by Ampsoft Tuesday, December 27, 2011 1:41 PM
    • Unproposed as answer by Ampsoft Tuesday, December 27, 2011 1:41 PM
    Monday, July 3, 2006 3:50 PM
  • Check to see if you are binding the event.
    If you migrated the application from 2k3 then it might be using the old fashioned method of binding the events.

    So if the OnLoad event is getting bound twice that would be about the only other way off the tip of my head that I know of that that an event could get fired twice unless a 2nd request was not being made.
    Monday, July 3, 2006 4:21 PM
    Moderator
  • Thanks,

    This was the problem.

    I solved it by setting AutoEventWireup to true in the @Page directive, then removing Handles MyBase.Page_Load, Me.Load from the Page_Load, in all the solutions files.

    Tuesday, July 4, 2006 1:08 PM
  • Thank You jake2004. I was having the same problem and after two days of trying everything, I happened across your case and it solved my problem too.
    Friday, March 16, 2007 2:01 AM
  • I had a similar problem with Page_Load firing twice. Fixed it by correcting the empty ImageURL for the (asp:Image) Tag to a known value. ..
    Tuesday, June 12, 2007 5:00 PM
  • Thanks a lot Jake2004. Your post helped me a lot .

     

     

    Monday, September 10, 2007 10:03 AM
  • Jake2004 thanks!

     

    Your comments did help resolve my problem. I did not set AutoEventWireup attribute to true, but I removed ",Me.Load" and that sovled my problem. Now I am investigating how and when ",Me.Load" was inserted into my code.

    -----------------------------------------------------------------------------------------

     

    Here is a note about AutoEventWireup attribute:

     

    When you set the value of the AutoEventWireup attribute to true, the ASP.NET runtime does not require events to specify event handlers like Page_Load or Page_Init. This means that the Handles keyword in Visual Basic .NET does not have to be used in the server script in the Web Form.

    By default, when the ASP.NET Web Application is created in Visual Studio .NET, the value of the AutoEventWireup attribute is set to false in the .aspx page and event handlers are automatically created. This article describes the default settings of the AutoEventWireup attribute and lists some helpful code.

     

    For more details about AutoEventWireup  please go to http://support.microsoft.com/kb/317690 

    Thursday, April 24, 2008 5:46 PM
  • I'd like to add my thanks to everyone.  This was an issue for me as well on a simple page that printed "ok" or an error message (an outside monitor for our website/database health). 

    In the C# code behind, I simply removed the line: 

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

    ignoring, of course, the dire warnings that I shouldn't mess with that line.  I realize that this is a vb.net forum, but I happened on it pretty quickly searching for page loading twice, so if I may muddy the pure vb waters with a little c# mud, I appreciate your help!

    Thursday, June 12, 2008 2:10 PM
  • This can happen when you have the AutoEventWireup page attribute set to "true" like this:

    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="true" Inherits="WebApplication1.WebForm1" %>

    AND you have the Load event being handled in the InitializeComponent() method this like:

    private void InitializeComponent()
    {   
         this.Load += new System.EventHandler(this.Page_Load);
    }

    I personally think it's kind of silly that they did the AutoEventWireup thing when they have a perfectly good, less magical, way to handle the event through the this.Load event object, especially when it can lead to issues like this.

    Dave
    Saturday, March 28, 2009 5:58 PM
  • My problem was a blank image source that I was setting programatically

    <asp:Image ID="myImage" runat="server" ImageUrl="" />

     Thanks MarcD
    Wednesday, July 15, 2009 11:10 PM
  • I had the same problem, and I had to remove the rogue ", Me.Load" from the page_load statement.

    I didn't touch the autoeventwireup settings.


    Thanks everyone who posted.

    Tuesday, August 25, 2009 12:23 PM
  • I had the same problem, and I had to remove the rogue ", Me.Load" from the page_load statement.

    I didn't touch the autoeventwireup settings.


    Thanks everyone who posted.

    Tuesday, August 25, 2009 12:23 PM
  • This is most likely happening because you have a tag that references a file that is blank.
    for example <img src="" /> will make a call to the Default.aspx. Usually it's a blank image tag. There is a possibility it could also be a <script or <style or <link tag also.


    This was exactly my problem and solved my issue!! 

    I had two similar pages, one was being called once, the other twice. The code was not that much different (as in asp/code behind). I finally came across this post and noticed i did have a placeholder image with no url "<img src="" width="100%" height="400px" />". I removed the image tag and the page only loaded once. I had no clue that a standard html would make the aspx be called twice.
    Monday, January 11, 2010 6:55 PM
  • I had a Similar issue and it turns out that my Page_load had an extra Handle

    OLD

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)  Handles MyBase.Load, Me.Load

    NEW

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)  Handles Me.Load

    Everything works now and my page Loads once.

    hope this helps.

    • Proposed as answer by NJDave71 Wednesday, December 29, 2010 3:19 PM
    Wednesday, December 29, 2010 3:04 PM