62,046
社区成员
发帖
与我相关
我的任务
分享
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 = this.Request.Form["__VIEWSTATE"];
_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 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();
}