处理asp.net页面刷新 [英] Handle page refresh in asp.net

查看:127
本文介绍了处理asp.net页面刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private Boolean IsPageRefresh = false;
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!IsPostBack)
        {
            ViewState["postids"] = System.Guid.NewGuid().ToString();
            Session["postid"] = ViewState["postids"].ToString();
            TextBox1.Text = "Hi";
    }
    else
    {
        if (ViewState["postids"].ToString() != Session["postid"].ToString())
        {
            IsPageRefresh = true;
        }
        Session["postid"] = System.Guid.NewGuid().ToString();
        ViewState["postids"] = Session["postid"];
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    if (!IsPageRefresh) // check that page is not refreshed by browser.
    {
        TextBox2.Text = TextBox1.Text + "@";

    }
}

我发现这个解决方案及其工作me.Buddy我能不明白,当提交页面然后查看状态变量和会话变量是一样的,之后我刷新页面然后查看,而最后一次状态和会话变量都differt值,他们具有相同的值。

i found this solution and its working for me.Buddy i could not understand that when page submitted then view state variable and session variable is same and after that i refresh page then view state and session variable have differt values while last time they have same value.

推荐答案

的想法很简单。

视图状态基本上是在形式的一些隐藏的输入。这个想法是检测页面刷新您提交表单后一度。这是prevent采取行动的两倍。

Viewstate is basically some hidden input in the form. The idea is to detect page refresh after you submit the form once. This is to prevent taking an action twice.

那么它是如何工作。结果
首先,当你创建一个形式,它具有1(例如)无论是在视图状态和会话。你提交之后,1是从视图状态检索和1是从会话检索:你获得 IsPageRefreshed ==虚假。在同一时间2被写入会话和向新视图状态

So how it works.
First, when you create a form it has "1" (for example) both in Viewstate and in Session. After you submit it, "1" is retrieved from Viewstate and "1" is retrieved from session: you get IsPageRefreshed==false. At the same time "2" is written to Session and to the new Viewstate.

让我们说,现在用户点击后退。在这种情况下,页面的HTML是浏览器的缓存中获取并ViewState中有1的值。如果现在提交表单,它有那么1视图状态和会话2: IsPageRefresh ==真

Let's say, now the user clicks "Back". In this case the HTML of the page is fetched from browser's cache and the Viewstate has a value of "1". If you submit the form now, it has then "1" in Viewstate and "2" in Session: IsPageRefresh==true

这篇关于处理asp.net页面刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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