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