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

    执行过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。

    另外,由于本方法会生成额外的Session占用服务器资源,所以请在必须保留当前页面状态的情况下使用,若无需保留当前页面状态,则在完成数据提交后直接重定向到当前页面即可。

SubmitOncePage

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

None.gif namespace  myControl
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 名称:SubmitOncePage 
InBlock.gif    
/// 父类:System.Web.UI.Page
InBlock.gif    
/// 描述:解决浏览器刷新造成的数据重复提交问题的page扩展类。
InBlock.gif    
/// 示例:    if (!this.IsRefreshed)
InBlock.gif    
///            {
InBlock.gif    
///                //具体代码
InBlock.gif    
///            }
InBlock.gif    
///    原创:丛兴滋(cncxz)    E-mail:cncxz@126.com
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class SubmitOncePage:System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
private string _strSessionKey;
InBlock.gif            
private string _hiddenfieldName;
InBlock.gif            
private string _strLastViewstate;
InBlock.gif           
InBlock.gif            
public SubmitOncePage()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                _hiddenfieldName 
= "__LastVIEWSTATE_SessionKey";
InBlock.gif                _strSessionKey 
= System.Guid.NewGuid().ToString();
InBlock.gif                _strLastViewstate 
= string.Empty;
ExpandedSubBlockEnd.gif            }

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

ExpandedSubBlockEnd.gif            }

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

InBlock.gif
InBlock.gif
InBlock.gif            
private string GetSessinKey()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string str = this.Request.Form[_hiddenfieldName];
InBlock.gif                
return (str == null? _strSessionKey : str;
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
private string GetSessinContent() dot.gif{
InBlock.gif                
string str = this.Request.Form["__VIEWSTATE"];
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (str == nulldot.gif{
InBlock.gif                    
return null;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
ExpandedSubBlockEnd.gif            }

InBlock.gif           
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif}

测试项目

    首先将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并添加测试代码,结果如下:

None.gif public   class  WebForm1 : myControl.SubmitOncePage
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
protected System.Web.UI.WebControls.Label Label1;
InBlock.gif        
protected System.Web.UI.WebControls.Button Button1;    
InBlock.gif        
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Button1.Click += new System.EventHandler(this.Button1_Click);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void Button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int i=int.Parse(Label1.Text)+1;
InBlock.gif            Label1.Text 
= i.ToString();
InBlock.gif            
if (!this.IsRefreshed) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                WriteFile(
"a.txt", i.ToString()); 
ExpandedSubBlockEnd.gif            }

InBlock.gif            WriteFile(
"b.txt", i.ToString());  
InBlock.gif        
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void WriteFile(string strFileName,string strContent)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string str = this.Server.MapPath(strFileName);       
InBlock.gif            System.IO.StreamWriter sw 
= System.IO.File.AppendText(str);
InBlock.gif            sw.WriteLine(strContent);
InBlock.gif            sw.Flush();
InBlock.gif            sw.Close();  
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif

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

相关文件下载
http://www.cnblogs.com/Files/cncxz/SubmitOncePage.rar         最后更新于 2005-12-28





posted on 2005-12-25 09:54  cncxz(虫虫) 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/cncxz/archive/2005/12/25/304141.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值