ASP.NET缓存页面.状态已保存,但需要刷新 [英] ASP.NET Cached page. State is saved but requires refresh

查看:46
本文介绍了ASP.NET缓存页面.状态已保存,但需要刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Webforms页面,其中大量使用了AJAX.上面有许多链接可以将用户带到另一个页面.如果用户使用浏览器的后退"按钮返回页面,我想保持页面所在的状态.

I have a webforms page that makes quite extensive use of AJAX. There are a number of links on it that take the user off to a different page. I want to maintain the state that the page was in should the user come back to it using the browser's back button.

我要做的是每次用户与页面互动时,将页面状态存储在会话中.当他们返回页面时,如果会话中存在状态,则会将其加载并显示给用户.

How I've gone about this is to store the page's state in session each time the user interacts with the page. When they return to the page, if a state exists in session, it is loaded and displayed to the user.

我有这种工作".该页面已正确保存,并且可以正确加载,但是很遗憾,需要刷新页面(F5)才能加载该页面,否则它会返回到先前的状态.

I have this "sort of" working. The page is saved correctly, and can be loaded correctly, but unfortunately requires a page refresh (F5) to load it, otherwise it falls back to a previous state.

您知道为什么没有刷新就无法正确呈现页面吗?我该如何解决这个问题?

Any idea why the page is not being rendered correctly without a refresh? How can I go about solving this?

protected void Page_Load(object sender, EventArgs e)
{
    Response.Cache.SetCacheability(HttpCacheability.NoCache);

    if (!Page.IsPostBack)
    {
        DataBindDropDowns();

        if (HasQueryString())
        {
            SetOptionsFromQueryString();
            SavePageToSession();
            Response.Redirect("Default.aspx");
        }
        else
        {
            RestoreFromSession();
            DisplayOptions();
        }
    }
}

推荐答案

您是否正在使用'load'事件初始化页面?当用户按下后退"按钮时,加载"事件不会触发,但"dom内容准备就绪"事件会触发.

Are you using the 'load' event to initialize the page? When a user presses the back button, the 'load' event doesn't fire but the 'dom content ready' event does.

请参阅:

点击后退按钮?

如果您使用的是jQuery,请尝试将页面初始化逻辑置于ready事件中,您应该会很好.如果您不使用jQuery,请找到合适的方法来连接DOM Content Loaded/Ready事件并使用它来初始化页面.

If you are using jQuery, try to put your page initialization logic in ready event and you should be good. If you are not using jQuery, find the appropriate way to hook up to the DOM Content Loaded/Ready event and use it to initialize your page.

如果仍然无法正常运行,请发布您的代码,以便我们调试问题.

If it still doesn't work, please post your code so that we can debug the problem.

好像您想设置适当的Expires标头,以便单击后退"按钮时浏览器将重新加载页面.请尝试以下页面上的代码:

Looks like you want to set appropriate Expires header so that browser will reload the page when back button is clicked. Try code from following page:

http://www.352media.com/rantingandraving/rave.aspx?raveid=325&id=212

这篇关于ASP.NET缓存页面.状态已保存,但需要刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆