您的位置:首页 > 其它

解决刷新页面造成的数据重复提交问题

2007-10-05 10:30 1061 查看
执行过postback操作的web页面在刷新的时候,浏览器会有“不重新发送信息,则无法刷新网页”的提示,若刚刚执行的恰好是往数据库插入一条新记录 的操作,点[重试]的结果是插入了两条重复的记录,以前一直是用保存数据后重新转向当前页面的方法解决,最近又找到了一个新的方法。
问题分析

在System.Web.UI.Page类中,有一个名为ViewState属性用以保存页面的当前视图状态,观察每个aspx页面最终生成的html代 码可以发现,其实就是向页面添加了一个名为__VIEWSTATE的隐藏域,其value值就是页面的当前状态,每次执行postback过后,该 value值都会发生变化,而刷新页面则不会改变。

针对这种情况,我们可以在页面代码执行的末尾将当前的ViewState写到一个Session中,而在页面加载时则判断该Session值是否与当前 ViewState相等(其实Session值恰好是ViewState的前一状态),若不等,则是正常的postback,若是相等则是浏览器刷新,这 样一来,只要在我们的数据插入代码外嵌套一个if判断就可以达到防止数据重复提交的目的了。

其实到这里问题还没有完全解决,具体说来就是Session的键值问题。假设我们将ViewState保存为this.Session ["myViewState"],如果一个用户同时打开两个防刷新提交的页面就乱套了,那针对页面的url设置Session的键值呢?还是不行,因为用 户有可能在两个窗口中打开同一页面,所以必须为每次打开的页面定义唯一的Session键值,并且该键值可以随当前页面实例一起保存,参考 ViewState的保存方式,我们直接向页面添加一个隐藏域专门存放Session键值就可以了。

经oop80和Edward.Net的提醒,为了尽可能地降低Session数据对服务器资源的占用量,现将上述方案略做调整,将ViewState利用md5加密后返回的32位字符串写入Session。

SubmitOncePage

SubmitOncePage是针对上述分析写的一个继承自System.Web.UI.Page的基类,需要防止刷新重复提交数据的页面从该基类继承,源码如下:


namespace myControl






{




/**//// <summary>


/// 名称:SubmitOncePage


/// 父类:System.Web.UI.Page


/// 描述:解决浏览器刷新造成的数据重复提交问题的page扩展类。


/// 示例: if (!this.IsRefreshed)


/// {


/// //具体代码


/// }


/// 原创:丛兴滋(cncxz) E-mail:cncxz@126.com


/// </summary>


public class SubmitOncePage:System.Web.UI.Page






{


private string _strSessionKey;


private string _hiddenfieldName;


private string _strLastViewstate;




public SubmitOncePage()






{


_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");


}




}


}
测试项目

首先将SubmitOncePage类的源码编译成一个单独的dll,然后进行测试,步骤如下:

1、新建一个asp.net web应用程序;
2、添加SubmitOncePage类对应的dll引用;
3、给webform1添加一个Label控件(Label1)和一个Button控件(Button1);
4、设置Label1的Text为0;
5、双击Button1转到codebehind视图;
6、修改类WebForm1的父类为SubmitOncePage并添加测试代码,结果如下:


public class WebForm1 : myControl.SubmitOncePage






{


protected System.Web.UI.WebControls.Label Label1;


protected System.Web.UI.WebControls.Button Button1;








Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码


override protected void OnInit(EventArgs e)






{


//


// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。


//


InitializeComponent();


base.OnInit(e);


}






/**//// <summary>


/// 设计器支持所需的方法 - 不要使用代码编辑器修改


/// 此方法的内容。


/// </summary>


private void InitializeComponent()






{


this.Button1.Click += new System.EventHandler(this.Button1_Click);


}


#endregion




private void Button1_Click(object sender, System.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();


}


}



7、按F5运行,在浏览器窗口中连续点击几次Button1,然后刷新几次页面,再点击几次Button1;
8、转到测试项目对应目录下,打开a.txt和b.txt文件,可看到if (!this.IsRefreshed) 的具体效果。

另外,由于本方法会生成额外的Session占用服务器资源,所以请在必须保留当前页面状态的情况下使用,若无需保留当前页面状态,则在完成数据提交后直接重定向到当前页面即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: