Page_Load is firing twice in ASP.NET page

前端 未结 16 1494
闹比i
闹比i 2020-12-08 09:57

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 loa

相关标签:
16条回答
  • 2020-12-08 10:14

    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.

    0 讨论(0)
  • 2020-12-08 10:15

    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.

    0 讨论(0)
  • 2020-12-08 10:17

    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.

    0 讨论(0)
  • 2020-12-08 10:17

    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.

    0 讨论(0)
  • 2020-12-08 10:18

    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 
    

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

    0 讨论(0)
  • 2020-12-08 10:18

    For me It was a blank image tag.

          <img src="#" />
    
    0 讨论(0)
提交回复
热议问题