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();  
        }
    }
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 " );
        }

    }
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值