45

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...

Rob
  • 6,731
  • 12
  • 52
  • 90
Goutham
  • 1,051
  • 3
  • 10
  • 8
  • 3
    An code example would be nice... And did you thought of the fact that there could be an AJAX-call going out somewhere twice to your page. Or maybe you're redirecting in and endless loop? – Rob Jan 24 '11 at 18:48
  • Please include any code that will help us see the issue. Plus, It is probably a __doPostBack call somewhere in your page code. – Doug Chamberlain Jan 24 '11 at 18:48
  • Hi bob and Doug, Thanks for your reply.. i checked.. there is not AJAX call and doPostBack calls in my code.. Do you guys know in how many ways can the page post back twice.. – Goutham Jan 24 '11 at 19:05
  • it's long slot. http://stackoverflow.com/questions/2009092/page-loads-twice-in-google-chrome – hIpPy May 17 '12 at 20:10

17 Answers17

33

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
5. Variation on the missing img src, body { background-image: url(); }
6. Rewrite rule and missing favicon.ico 
7. Page uses ScriptManager and "initializes" the loading panel by issuing a 
   postback from javascript.

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...

jmoreno
  • 12,752
  • 4
  • 60
  • 91
  • 2
    I turned AutoEventWireUp to false in my master page and it helped me. Removing it did not. – AlignedDev Aug 14 '13 at 14:53
  • this was exactly my issue. Inherited from a base class. I removed the handles class from the child class and it stopped firing repeatedly. – user158017 Sep 09 '13 at 01:01
  • My problem was all of these things and more. Once I cleaned up the code, switched the pages and master to AutoEventWireup="true", and deleted all of the page load event handlers in the master and the pages, I then had to change the namespace of my master to be the same as the project. – Marnee KG7SIO Feb 17 '14 at 20:10
  • https://forums.asp.net/post/5052415.aspx , for me img tag src="#" caused the page_load fired twice – Pranesh Janarthanan Mar 07 '18 at 10:47
  • Thank you! Turning off AutoEventWireup worked. – davelabaw Sep 07 '22 at 16:13
18

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 
}
Julius A
  • 38,062
  • 26
  • 74
  • 96
  • 1
    Wow I remember this from the 10 years ago ! – Preet Sangha Jul 18 '12 at 11:37
  • 8
    IsPostback not an answer. Redirect from an external site appears to cause two loads that are not postbacks. Spent a day going nuts on this. Ended up compensating down stream. – Bob Clegg Feb 04 '13 at 18:44
  • 1
    Lifesaver, thank you Julius. This means that the 1st Page_Load to init the content, 2nd Page_Load to execute any code in (IsPostBack) block, then any event handlers after that. – mn. Jul 03 '15 at 05:24
  • So sad that I am encountering this issue in 2022. I feel old and not moving with the latest on technology. – Edgar J. Rodriguez Jul 24 '22 at 00:09
11

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.

DisplayName
  • 3,093
  • 5
  • 35
  • 42
sajeeth.S
  • 111
  • 1
  • 2
8

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.

Alexey F
  • 1,763
  • 14
  • 19
  • 2
    Experienced the same thing with a tag. The tricky part was that the extra request (a GET to the same page) doesn't show up in Chrome Dev Tools. Not sure why or how that is possible. Aside: the only reason I had a "#" src'd image in the first place is because IE8 was giving me issues when manipulating the DOM of a . – ironsam Mar 25 '14 at 21:37
  • 1
    I had this same thing, but I had a "valid" src value. I hadn't added a preceding "/". The src value was "resources/images/button.png", once changed to "/resources/images/button.png", only a single page_load. If someone can explain why, I'd be very interested. Thanks – Ads Apr 01 '14 at 13:07
7

For me It was a blank image tag.

      <img src="#" />
vinayak hegde
  • 2,117
  • 26
  • 26
4

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.

  • 2
    The problem I have is when I set AutoEventWriteUp to FALSE, the page_load event doesn't fire at all now. – Micro Oct 14 '14 at 20:22
4

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"

Ray
  • 41
  • 1
2

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.

  • 1
    This worked for me. I changed the url from "/preload.aspx" to "/preload.aspx/" , and Page_Load is now called only once. – Gandalf458 Feb 04 '22 at 21:05
2

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

DotNET
  • 55
  • 8
1

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

Doug Chamberlain
  • 11,192
  • 9
  • 51
  • 91
0

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

bruno leclerc
  • 333
  • 2
  • 9
0

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.

David d C e Freitas
  • 7,481
  • 4
  • 58
  • 67
0

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.

Community
  • 1
  • 1
  • Response.Redirect and Server.Transfer serve different purposes and are not 100% interchangeable. What made you think it was appropriate to change a Redirect to a Transfer in all cases? – jmoreno Jul 01 '15 at 03:57
0

For me it was solved by removing

Handles Me.Load 

and changing the method like

Protected Overrides OnLoad(...)
sertsedat
  • 3,490
  • 1
  • 25
  • 45
0

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.

Anke
  • 141
  • 1
  • 4
0

I had same issue. I have set AutoEventWireup to false and after that remove the Page.Load += new EventHandler(Page_Load); from the InitializeComponent(). Now it's working..... I think because of [DefaultEvent("Load")] in System.Web.UI.Page, the default event is Load so you don't need to add it again when your page class is initializing.

Ramin
  • 19
  • 5
0

I am also experiencing page loading twice. For me, I have deduced that is happening only when aspx page contains <style> with internal css declaration. If I remove that tag, then it loads only once.