C# 为什么 Page_Load 在使用 ASP.NET 从另一个页面返回后没有触发 - ergo 史诗般的尴尬 :)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2281305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:00:20  来源:igfitidea点击:

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

c#asp.neteventsresponse.redirect

提问by G Berdal

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

假设我在同一个 ASP.NET C# 网站上有两个页面。

  • Page1.aspx does things in the Page_Load event
  • I navigate to Page2.aspx using the menu
  • Page2.aspx does some things then Response.Redirectback to Page1.aspx
  • Page1.aspx cannot do things in Page_Load event this time because it never fires.
  • Page1.aspx 在 Page_Load 事件中做事
  • 我使用菜单导航到 Page2.aspx
  • Page2.aspx 做了一些事情然后Response.Redirect回到 Page1.aspx
  • 这次 Page1.aspx 不能在 Page_Load 事件中做任何事情,因为它永远不会触发。

I tried to turn off cache declaratively, tried using true for endResponse in my redirect... nothing seems to make a difference.

我试图以声明方式关闭缓存,尝试在我的重定向中对 endResponse 使用 true ......似乎没有任何区别。

Never mind everybody! I am a moron! Using Visual Studio Dev Localhost the Redirect was redirecting to the live page! :)

不用管大家!我是个白痴!使用 Visual Studio Dev Localhost 重定向重定向到实时页面!:)

采纳答案by Guffa

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

页面执行的原因不影响页面周期,当页面执行时,Load 事件总是会触发。

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.

因此,如果 Page_Load 有时不运行,那是因为页面被缓存并且不在服务器上执行。该页面可以缓存在浏览器中,沿途某个地方的路由器中,或者使用服务器端页面缓存的服务器上。

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

这将防止页面在正常情况下被缓存。(还要检查您的浏览器是否处于离线模式,然后无论它的可缓存性设置如何,它都会使用缓存中的任何内容。)

回答by SLaks

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.

您可以使用Fiddler确认这一点。

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

我不确定这是否适用于所有浏览器。

回答by NotMe

If you are redirecting, it's possible the client is caching the response. In order to get past this you might add an extra query parameter that simply holds the time.

如果您正在重定向,则客户端可能正在缓存响应。为了解决这个问题,您可能会添加一个额外的查询参数来保存时间。

This is usually enough to get past most pages caching mechanisms.

这通常足以通过大多数页面缓存机制。

回答by D3no

Try using Server.Transfer instead of Response.Redirect.

尝试使用 Server.Transfer 而不是 Response.Redirect。

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

客户端不会看到 URL 更改,但这可能无关紧要,具体取决于您的要求

回答by EBarro

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

我遇到了同样的问题,发现这对我有用:(在 Page_Load 部分添加这个)

    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.

然后我在它执行我想要运行的代码后立即添加 Session["RELOAD"] = "1" 以防止它一遍又一遍地刷新。奇迹般有效。

回答by vinayak hegde

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

将 VS 从调试模式更改为发布模式对我有用....

回答by Vikas Ranjan Rai

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

请运行以下代码以禁用 Firefox 中的页面缓存。

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

Apply this in page load of master page.

将此应用于母版页的页面加载。