zoukankan      html  css  js  c++  java
  • 解决刷新页面造成的数据重复提交问题

    1.新建一个pagebase类,然后让页面继承pagebase,

    2.新建一个页面,继承PageBase,然后面页面添加一个Label1且Text属性为0;

    3.添加一个Button,然后运行页面,点击button二次,然后,再按F5刷新页面三次,

    4.查看根目录下的a.txt,和b.txt

    效果值为 a.txt : 1 ,2   b.txt :1,2,2,2,2  说明页面刷新时执行了b.txt

    代码如下:

    public class PageBase : System.Web.UI.Page
        {
            
    private string _strSessionKey;
            
    private string _hiddenfieldName;
            
    private string _strLastViewstate;

            
    public PageBase()
            {
                _hiddenfieldName 
    = "__LastVIEWSTATE_SessionKey";
                _strSessionKey 
    = System.Guid.NewGuid().ToString();
                _strLastViewstate 
    = string.Empty;
            }

            
    public bool IsRefreshed
            {
                
    get
                {
                    
    string str1 = GetSessinContent();
                    _strLastViewstate 
    = str1;
                    
    string str2 = this.Session[GetSessinKey()] as string;
                    
    bool flag1 = (str1 != null&& (str2 != null&& (str1 == str2);
                    
    return flag1;
                }
            }

            
    protected override void Render(System.Web.UI.HtmlTextWriter writer)
            {
                
    string str = GetSessinKey();
                
    this.Session[str] = _strLastViewstate;
                
    this.RegisterHiddenField(_hiddenfieldName, str);
                
    base.Render(writer);
            }


            
    private string GetSessinKey()
            {
                
    string str = this.Request.Form[_hiddenfieldName];
                
    return (str == null? _strSessionKey : str;
            }

            
    private string GetSessinContent()
            {
                
    string str = this.Request.Form["__VIEWSTATE"];
                
    if (str == null)
                {
                    
    return null;
                }
                
    return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
            }

        }

    页面代码:

    protected void Page_Load(object sender, EventArgs e)
            {
                
    int i = int.Parse(Label1.Text) + 1;
                Label1.Text 
    = i.ToString();
                
    if (!this.IsRefreshed)
                {
                    WriteFile(
    "a.txt", i.ToString());
                }
                WriteFile(
    "b.txt", i.ToString());  

            }
            
    private void WriteFile(string strFileName, string strContent)
            {
                
    string str = this.Server.MapPath(strFileName);
                System.IO.StreamWriter sw 
    = System.IO.File.AppendText(str);
                sw.WriteLine(strContent);
                sw.Flush();
                sw.Close();
            }
  • 相关阅读:
    2013.10.21—2013.10.25周总结
    2013.10.14—2013.10.18周总结
    2013.10.8—2013.10.12周总结
    MongoDb的“not master and slaveok=false”错误及解决方法,读写分离
    python 获取当前时间
    git命令与github使用
    s​s​h​配​置​公​钥​和​私​钥​登​陆​S​e​c​u​r​e​C​R​T
    关于pydev的语法的错误提示
    lnmp1.0 升级php.5.4.28 后出错 Nginx 502 Bad Gateway
    python线程Example
  • 原文地址:https://www.cnblogs.com/wenming205/p/1569430.html
Copyright © 2011-2022 走看看