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

孤街浪徒 提交于 2019-11-29 04:38:38

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();

        }

    }

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!