Page_Load is firing twice in ASP.NET page

前端 未结 16 1496
闹比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:27

    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

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

    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 
    }
    
    0 讨论(0)
  • 2020-12-08 10:32

    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.

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

    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"

    0 讨论(0)
提交回复
热议问题