ASP.NET browser shows “web page has expired” for back button (after a post back)

前端 未结 2 1651
遥遥无期
遥遥无期 2020-12-17 01:42

I\'m having trouble with a simple ASP.NET application and the back button after a post back.

The page in question has a simple form on it, some text fields etc, and

相关标签:
2条回答
  • 2020-12-17 02:01

    What you're dealing with is actually an old problem. In essence, the reason that you're seeing the "web page has expired" message is that one of the techniques for disabling the "back" button has been employed. The technique sets the cache to a date in the past, therefore causing the browser to show this error if the user clicks the "back" button.

    That would be this line of code:

    Response.Cache.SetExpires(DateTime.Now.AddMinutes(-1)); 
    

    This has been an issue, particularly with WebForms ASP.NET because of how the postback works, compared to other frameworks.

    For a thorough explanation of all the issues involved, I strongly recommend reading the article linked to below. It does not answer your question directly, but I think you will get more information out of it than a simple answer and will help you think through your options, armed with a better understanding of the issue at hand. Be sure to read parts 1 AND 2.

    http://www.4guysfromrolla.com/webtech/111500-1.shtml

    I do have an idea on how to make the "back" button behave like a "back" button again, so that postbacks aren't treated as a page navigation:

    Personally, I've adopted a (arguably hackish/sloppy) approach of just putting things in an UpdatePanel when I don't want the postbacl/back button conflict, since I use Ajax in most of my apps anyway. This forces the "back" button to actually go back to the previous page, rather than staing on the same page, but reverting to the control values as they were before the postback.

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

    Depending on a situation you might get away with this hack/workaround:

     private void Page_PreRender(object sender, System.EventArgs e)
        {
            if (IsPostBack && !IsCallback)
            {
    
                Response.Write("<html><head><script>location.replace('" + Request.Path + "');\n" + "</script></head><body></body></html>\n");
    
                Response.End();
    
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题