防止刷新提交页面!
方法一:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public class BasePage : System.Web.UI.Page
{
private readonly string REFRESH_TICKET_NAME = "__RefreshTicketArray";
private readonly string HIDDEN_FIELD_NAME = "__RefreshHiddenField";
private readonly string HIDDEN_PAGE_GUID = "__RefreshPageGuid";
/// <summary>
/// 为True表示页面刷新,False为正常提交
/// </summary>
public bool IsPageRefreshed
{
get
{
if (IsPostBack && !CheckRefreshFlag())
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 呈现前更新标识
/// </summary>
/// <param name="e"></param>
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
UpdateRefreshFlag();
}
/// <summary>
/// 更新标识,正常提交都删除该次提交的时间,并生产当前新的时间
/// </summary>
private void UpdateRefreshFlag()
{
#region Cookie模式
//注册页面唯一标识并返回
string pageGuid = SetCurPageGuid();
HttpCookie cookie = GetRefreshTicket();
if (cookie.Values.Count > 0)
{
cookie.Values.Remove(pageGuid);
}
string submitTime = DateTime.Now.ToString("hhmmss.fffff");
//当前提交时间保存到隐藏域
ClientScript.RegisterHiddenField(HIDDEN_FIELD_NAME, submitTime);
cookie.Values.Add(pageGuid, submitTime);
Response.AppendCookie(cookie);
#endregion
}
/// <summary>
/// 验证是否刷新
/// </summary>
/// <returns></returns>
private bool CheckRefreshFlag()
{
HttpCookie cookie = GetRefreshTicket();
string pageGuid = GetCurPageGuid();
if (cookie.Values.Count > 0)
{
bool flag;
if (cookie.Values[pageGuid] != null)
flag = cookie.Values[pageGuid].IndexOf(GetCurSubmitTime()) > -1;
else
flag = true;//防止出现异常,总是可以提交
return flag;
}
return true;
}
/// <summary>
/// 得到已保存的提交时间,没有新建,有返回
/// </summary>
/// <returns></returns>
private HttpCookie GetRefreshTicket()
{
#region Cookie模式,返回值为Cookie
HttpCookie cookie;
if (Request.Cookies[REFRESH_TICKET_NAME] == null)
{
cookie = new HttpCookie(REFRESH_TICKET_NAME);
Response.AppendCookie(cookie);
}
else
{
cookie = Request.Cookies[REFRESH_TICKET_NAME];
}
return cookie;
#endregion
}
/// <summary>
/// 获取当前提交时间
/// </summary>
/// <returns></returns>
private string GetCurSubmitTime()
{
string submitTime = Request.Params[HIDDEN_FIELD_NAME] == null ? "" : Request.Params[HIDDEN_FIELD_NAME].ToString();
return submitTime;
}
/// <summary>
/// 设置页面唯一标识,通过Guid标识来区分每个页面自己的提交时间
/// </summary>
private string SetCurPageGuid()
{
string guid;
if (!IsPostBack)
{
if (Request.Params[HIDDEN_PAGE_GUID] == null)
{
guid = System.Guid.NewGuid().ToString();
}
else
guid = GetCurPageGuid();
}
else
{
guid = GetCurPageGuid();
}
ClientScript.RegisterHiddenField(HIDDEN_PAGE_GUID, guid);
return guid;
}
/// <summary>
/// 得到当前页面的唯一标识
/// </summary>
/// <returns></returns>
private string GetCurPageGuid()
{
string pageGuid = Request.Params[HIDDEN_PAGE_GUID] == null ? "none" : Request.Params[HIDDEN_PAGE_GUID].ToString();
return pageGuid;
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public class BasePage : System.Web.UI.Page
{
private readonly string REFRESH_TICKET_NAME = "__RefreshTicketArray";
private readonly string HIDDEN_FIELD_NAME = "__RefreshHiddenField";
private readonly string HIDDEN_PAGE_GUID = "__RefreshPageGuid";
/// <summary>
/// 为True表示页面刷新,False为正常提交
/// </summary>
public bool IsPageRefreshed
{
get
{
if (IsPostBack && !CheckRefreshFlag())
{
return true;
}
else
{
return false;
}
}
}
/// <summary>
/// 呈现前更新标识
/// </summary>
/// <param name="e"></param>
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
UpdateRefreshFlag();
}
/// <summary>
/// 更新标识,正常提交都删除该次提交的时间,并生产当前新的时间
/// </summary>
private void UpdateRefreshFlag()
{
#region Cookie模式
//注册页面唯一标识并返回
string pageGuid = SetCurPageGuid();
HttpCookie cookie = GetRefreshTicket();
if (cookie.Values.Count > 0)
{
cookie.Values.Remove(pageGuid);
}
string submitTime = DateTime.Now.ToString("hhmmss.fffff");
//当前提交时间保存到隐藏域
ClientScript.RegisterHiddenField(HIDDEN_FIELD_NAME, submitTime);
cookie.Values.Add(pageGuid, submitTime);
Response.AppendCookie(cookie);
#endregion
}
/// <summary>
/// 验证是否刷新
/// </summary>
/// <returns></returns>
private bool CheckRefreshFlag()
{
HttpCookie cookie = GetRefreshTicket();
string pageGuid = GetCurPageGuid();
if (cookie.Values.Count > 0)
{
bool flag;
if (cookie.Values[pageGuid] != null)
flag = cookie.Values[pageGuid].IndexOf(GetCurSubmitTime()) > -1;
else
flag = true;//防止出现异常,总是可以提交
return flag;
}
return true;
}
/// <summary>
/// 得到已保存的提交时间,没有新建,有返回
/// </summary>
/// <returns></returns>
private HttpCookie GetRefreshTicket()
{
#region Cookie模式,返回值为Cookie
HttpCookie cookie;
if (Request.Cookies[REFRESH_TICKET_NAME] == null)
{
cookie = new HttpCookie(REFRESH_TICKET_NAME);
Response.AppendCookie(cookie);
}
else
{
cookie = Request.Cookies[REFRESH_TICKET_NAME];
}
return cookie;
#endregion
}
/// <summary>
/// 获取当前提交时间
/// </summary>
/// <returns></returns>
private string GetCurSubmitTime()
{
string submitTime = Request.Params[HIDDEN_FIELD_NAME] == null ? "" : Request.Params[HIDDEN_FIELD_NAME].ToString();
return submitTime;
}
/// <summary>
/// 设置页面唯一标识,通过Guid标识来区分每个页面自己的提交时间
/// </summary>
private string SetCurPageGuid()
{
string guid;
if (!IsPostBack)
{
if (Request.Params[HIDDEN_PAGE_GUID] == null)
{
guid = System.Guid.NewGuid().ToString();
}
else
guid = GetCurPageGuid();
}
else
{
guid = GetCurPageGuid();
}
ClientScript.RegisterHiddenField(HIDDEN_PAGE_GUID, guid);
return guid;
}
/// <summary>
/// 得到当前页面的唯一标识
/// </summary>
/// <returns></returns>
private string GetCurPageGuid()
{
string pageGuid = Request.Params[HIDDEN_PAGE_GUID] == null ? "none" : Request.Params[HIDDEN_PAGE_GUID].ToString();
return pageGuid;
}
}
方法二:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
public class BasePage : System.Web.UI.Page
{
public BasePage()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region IsRefreshed
/// <summary>
/// IsRefreshed
/// </summary>
public bool IsRefreshed
{
get
{
string requestPath = HttpContext.Current.Request.Path;
return (bool)HttpContext.Current.Items[requestPath + "_IsRefreshed"];
}
}
#endregion
///
/// 重写OnPreRenderComplete,生成隐藏域CurrentTicket
///
///
protected override void OnPreRenderComplete(EventArgs e)
{
base.OnPreRenderComplete(e);
string requestPath = HttpContext.Current.Request.Path;
int ticket = int.Parse(HttpContext.Current.Items[requestPath + "_NextTicket"].ToString());
ClientScript.RegisterHiddenField("CurrentTicket", ticket.ToString());
}
}
/// <summary>
/// NoRepeatOperModule 的摘要描述
/// </summary>
public class NoRepeatOperModule : IHttpModule
{
public NoRepeatOperModule()
{
//
// TODO:
//
}
/// <summary>
/// 保存访问的页面记录,以页面路径为key, 标号为value
/// </summary>
static Hashtable historyRequest = null;
/// <summary>
/// 请求路径
/// </summary>
private string RequestPath
{
get
{
return HttpContext.Current.Request.Path;
}
}
#region IHttpModule 成员
public void Dispose()
{
//throw new Exception("The method or operation is not implemented.");
}
public void Init(HttpApplication context)
{
if (historyRequest == null)
{
historyRequest = new Hashtable();
}
context.BeginRequest += delegate(object sender, EventArgs e)
{
int lastTicket = GetLastTicket();
int currentTicket = GetCurrentTicket(lastTicket);
// 比较当前标号和上一个的标号,判断页面请求是否来源于刷新操作
// 当前标号大于上一个标号 或初次请求都属于新的页面
if (currentTicket > lastTicket || (lastTicket == currentTicket && currentTicket == 0))
{
// 标记并保存页面请求是否来源于刷新的bool值
HttpContext.Current.Items[RequestPath + "_IsRefreshed"] = false;
historyRequest[RequestPath] = currentTicket;
}
else
{
HttpContext.Current.Items[RequestPath + "_IsRefreshed"] = true;
}
};
}
#endregion
/**/
/// <summary>
/// 获取某页面的上一个标号
/// </summary>
/// <returns></returns>
private int GetLastTicket()
{
if (!historyRequest.ContainsKey(RequestPath))
{
return 0;
}
return int.Parse(historyRequest[RequestPath].ToString());
}
/**/
/// <summary>
/// 获取页面的当前标号
/// </summary>
/// <param name="lastTicket">上一个标号</param>
/// <returns></returns>
private int GetCurrentTicket(int lastTicket)
{
int ticket;
// CurrentTicket 为页面的隐藏域的内容
string currentTicket = HttpContext.Current.Request["CurrentTicket"];
if (currentTicket == null)
{
ticket = lastTicket;
}
else
{
ticket = int.Parse(currentTicket);
}
// 保存页面的下一个标号
HttpContext.Current.Items[RequestPath + "_NextTicket"] = ticket + 1;
return ticket;
}
}
//webconfig配置
//<httpModules>
// <add name="NoRepeatOperModule" type="NoRepeatOperModule" />
//</httpModules>
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
public class BasePage : System.Web.UI.Page
{
public BasePage()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region IsRefreshed
/// <summary>
/// IsRefreshed
/// </summary>
public bool IsRefreshed
{
get
{
string requestPath = HttpContext.Current.Request.Path;
return (bool)HttpContext.Current.Items[requestPath + "_IsRefreshed"];
}
}
#endregion
///
/// 重写OnPreRenderComplete,生成隐藏域CurrentTicket
///
///
protected override void OnPreRenderComplete(EventArgs e)
{
base.OnPreRenderComplete(e);
string requestPath = HttpContext.Current.Request.Path;
int ticket = int.Parse(HttpContext.Current.Items[requestPath + "_NextTicket"].ToString());
ClientScript.RegisterHiddenField("CurrentTicket", ticket.ToString());
}
}
/// <summary>
/// NoRepeatOperModule 的摘要描述
/// </summary>
public class NoRepeatOperModule : IHttpModule
{
public NoRepeatOperModule()
{
//
// TODO:
//
}
/// <summary>
/// 保存访问的页面记录,以页面路径为key, 标号为value
/// </summary>
static Hashtable historyRequest = null;
/// <summary>
/// 请求路径
/// </summary>
private string RequestPath
{
get
{
return HttpContext.Current.Request.Path;
}
}
#region IHttpModule 成员
public void Dispose()
{
//throw new Exception("The method or operation is not implemented.");
}
public void Init(HttpApplication context)
{
if (historyRequest == null)
{
historyRequest = new Hashtable();
}
context.BeginRequest += delegate(object sender, EventArgs e)
{
int lastTicket = GetLastTicket();
int currentTicket = GetCurrentTicket(lastTicket);
// 比较当前标号和上一个的标号,判断页面请求是否来源于刷新操作
// 当前标号大于上一个标号 或初次请求都属于新的页面
if (currentTicket > lastTicket || (lastTicket == currentTicket && currentTicket == 0))
{
// 标记并保存页面请求是否来源于刷新的bool值
HttpContext.Current.Items[RequestPath + "_IsRefreshed"] = false;
historyRequest[RequestPath] = currentTicket;
}
else
{
HttpContext.Current.Items[RequestPath + "_IsRefreshed"] = true;
}
};
}
#endregion
/**/
/// <summary>
/// 获取某页面的上一个标号
/// </summary>
/// <returns></returns>
private int GetLastTicket()
{
if (!historyRequest.ContainsKey(RequestPath))
{
return 0;
}
return int.Parse(historyRequest[RequestPath].ToString());
}
/**/
/// <summary>
/// 获取页面的当前标号
/// </summary>
/// <param name="lastTicket">上一个标号</param>
/// <returns></returns>
private int GetCurrentTicket(int lastTicket)
{
int ticket;
// CurrentTicket 为页面的隐藏域的内容
string currentTicket = HttpContext.Current.Request["CurrentTicket"];
if (currentTicket == null)
{
ticket = lastTicket;
}
else
{
ticket = int.Parse(currentTicket);
}
// 保存页面的下一个标号
HttpContext.Current.Items[RequestPath + "_NextTicket"] = ticket + 1;
return ticket;
}
}
//webconfig配置
//<httpModules>
// <add name="NoRepeatOperModule" type="NoRepeatOperModule" />
//</httpModules>