Why is Page_Load not firing after coming back from another page using ASP.NET - ergo epic embarrassment :)

前端 未结 7 1786
故里飘歌
故里飘歌 2021-02-14 22:15

Let\'s say I have two pages on the same ASP.NET C# WebSite.

  • Page1.aspx does things in the Page_Load event
  • I navigate to Page2.aspx using the menu
相关标签:
7条回答
  • 2021-02-14 22:52

    When you navigate to a page using the Back button, the page is reloaded from memory, and no request is sent to the server.

    You can confirm this using Fiddler.

    I'm not sure if this is true in all browsers.

    0 讨论(0)
  • 2021-02-14 22:53

    Please run the following code to disable the page cache in firefox.

    Response.AppendHeader("Cache-Control", "no-store");
    

    Apply this in page load of master page.

    0 讨论(0)
  • 2021-02-14 22:55

    The reason for the page executing doesn't affect the page cycle, the Load event always fires when the page is executed.

    So, if the Page_Load doesn't run sometimes, it's because the page is cached and doesn't execute on the server. The page can be cached in the browser, in a router somewhere along the way, or on the server using server side page caching.

    If you haven't enabled server side page caching for the page, it's cached in the browser or in the network. You can use cache settings to try to elliminate this:

    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    

    This will keep the page from being cached in normal circumstances. (Check also that your browser isn't in offline mode, then it will use anything in the cache regardless of it's cacheability settings.)

    0 讨论(0)
  • 2021-02-14 22:59

    Try using Server.Transfer instead of Response.Redirect.

    The client will not see the URL change but this may not matter, depending on your requirements

    0 讨论(0)
  • 2021-02-14 22:59

    Changing VS from debug to Release mode worked for me ....

    0 讨论(0)
  • 2021-02-14 23:08

    I had the same problem and found that this works for me: (add this on the Page_Load section)

        if (this.Master.Page.Header != null && Session["RELOAD"] == null)
        {
            System.Web.UI.HtmlControls.HtmlHead hh = this.Master.Page.Header;
            System.Web.UI.HtmlControls.HtmlMeta hm = new System.Web.UI.HtmlControls.HtmlMeta();
            hm.Attributes.Add("http-equiv", "Refresh");
            hm.Attributes.Add("content", "3");
            hh.Controls.Add(hm);
        }
    

    and then I add Session["RELOAD"] = "1" right after it executes the code I want to run to prevent it from refreshing over and over again. Works like a charm.

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