Page_Load is firing twice in ASP.NET page

2019-01-17 06:13发布

问题:

Asp.net page_load function is loading twice.. hence it affects my page performance. Does anyone know the reason it is loading twice.

No, iam not calling the page load function anywhere...

回答1:

Just ran into this problem, and thought I would post an answer summarizing what I found, plus my actual issue.

1. img tags with src="" or Image tags with ImageUrl="
2. Using AutoEventWireup="true" and adding a page handler
3. Having manually added the event handler (more common for C# than VB)
4. Handling both MyBase.Load and Me.Load

and finally my issue....

My page inherited from a class that included a Page Load handler, which inherited from a class with a Page Load Handler.

Public Class C1
    Inherits System.Web.UI.Page
   Protected Overridable Sub PageLoad(ByVal sender As Object, 
                               ByVal e As System.EventArgs) Handles Me.Load
   End Sub
End Class

Public Class C2
    Inherits C1
    Protected Overrides Sub PageLoad(ByVal sender As Object, 
                      ByVal e As System.EventArgs) Handles Me.Load
        MyBase.PageLoad(sender, e)
    End Sub
End Class

Public Class MyPage 
    Inherits C2
    Protected Overrides Sub PageLoad(ByVal sender As Object, 
                      ByVal e As System.EventArgs) 
        MyBase.PageLoad(sender, e)
    End Sub
End Class

I tested this, and if you put a Handles on the method in MyPage, it will get hit 3 times...



回答2:

It is not you calling the page load function twice, that is the way ASP.NET works. The page posts to itself, thus calling the page_load function, when any Server controls on the page are fired (those which as set to postback).

What you need to do is to put some checks to differentiate between an initial page load and a post back

if(!IsPostBack) 
{  
//Code when initial loading 
}
 else 
{ 
// code when post back 
}


回答3:

Please find the solution here........

  1. Check if the Load events have Handlers for Base class and the child class Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load**

  2. If it is just Remove the Me.Load from the event , now check your page. Hope this may be useful and solve your issue.



回答4:

Once I found the following string in a project:

  <link rel="Shortcut Icon" href="#" type="image/x-icon" />

Somebody just did it like he usually does with "a href". But browser actually tries to get the site icon on each refresh, so it sends a request to the address from href parameter, i.e. to the same page.

So, check this as well.



回答5:

For me It was a blank image tag.

      <img src="#" />


回答6:

Remember to check the IsPostBack value as shown below:

    protected void Page_Load(object sender, EventArgs e)
    {
            if (!this.IsPostBack)

You can put breakpoints inside this IF block to verify you running Page_Load twice. If you are seeing Page_Load run twice and each time it is not a postback, then check the OnInit() method for this page. Verify you are not wiring up the Load handler like below. You will see this code often from code that was migrated from earlier versions of Visual Studio.

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

Remove this if you find it. This assumes that you have the following at the top of the markup for the page. AutoEventWireup="true"



回答7:

I solved my issue by setting the AutoEventWireUp attribute to FALSE. I got this issue when migrating from .net 1.1 to .net 4.0. Somehow VS2012 reset this attribute to TRUE when I copy the file over from the older version.



回答8:

I had the same problem and Solved.

I Checked my Global.ascx and My rewrite rules.

When the page requested, URL did not have "/" at the end of URL and a redirect happened from "x.com/x" to "x.com/x/" according to my configuration for SEO standards.

So anything works well and your internal links should have "/" at the end of URLs to avoid multiple loads.



回答9:

The Page object calls the OnLoad method on the Page object, and then recursively does the same for each child control until the page and all controls are loaded. The Load event of individual controls occurs after the Load event of the page.

Use the OnLoad event method to set properties in controls and to establish database connections.

Reffer MSDN: enter link description here



回答10:

Please try making the changes mentioned in this link. http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/ccc75925-3460-497c-8471-fcebecd8d061

BTW I googled Page_Load Being called twice



回答11:

I had the same issue. It was because of a TreeNode with ImageUrl="".



回答12:

For me I could get around this calling multiple times by using the PreRender event instead

protected override void OnPreRender(EventArgs e)

This is only called once, even if the onload's and init's are called a million times.



回答13:

I replaced the Response.Redirect with Server.Transfer because it was suggested to be the new way of doing things. Since then, the pages load twice and the Back-button in Chrome returns to the previous page and immediately back to the current page. I replaced the Server.Transfer with Response.Redirect, and all was back to normal. I also put this answer on page loads twice due to js code.



回答14:

For me it was solved by removing

Handles Me.Load 

and changing the method like

Protected Overrides OnLoad(...)


回答15:

For me, this issue cropped up suddenly after the Oct. 2017 Windows update. I noticed that for pages made accessible to anonymous users via a Location element in web.config, it is now necessary to also grant access to any assets referenced by that page, for example images, stylesheets, etc. The below example grants anonymous access to the login page and the 'images' directory (aka folder):

<location path="login.aspx">
  <system.web>
    <authorization>
      <allow users="?" />
    </authorization>
  </system.web>
</location>
<location path="images">
  <system.web>
    <authorization>
      <allow users="?" />
    </authorization>
  </system.web>
</location>

Update: I found a second cause of Page_Load being called twice. In old, legacy code, some pages' .aspx.designer.cs files contained inconsistencies that apparently hadn't caused problems until now. Instead of attempting repair, I created new pages, which eliminated the double load event.