Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page_Load is firing twice in ASP.NET page

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

like image 568
Goutham Avatar asked Jan 24 '11 18:01

Goutham


People also ask

What is Page_Load method in asp net?

Your web form has a method called Page_Load. This method is called each time your page is loaded. This includes: 1. The first time the page is loaded, e.g., when a user enters the url to it or clicks on a link to it.

What is IsPostBack in asp net with example?

IsPostBack is a property of the Asp.Net page that tells whether or not the page is on its initial load or if a user has perform a button on your web page that has caused the page to post back to itself.

What is AutoEventWireup in asp net?

AutoEventWireup is an attribute in Page directive. AutoEventWireup is a Boolean attribute that indicates whether the ASP.NET pages events are auto-wired. AutoEventWireup will have a value true or false. By default it is true.


2 Answers

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

like image 156
jmoreno Avatar answered Sep 28 '22 04:09

jmoreno


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  } 
like image 38
Julius A Avatar answered Sep 28 '22 05:09

Julius A