捕捉刷新事件
参考了以下文章
http://msdn2.microsoft.com/en-us/library/ms379557(VS.80).aspx
做了一些改进,把计数器放到Cookie中,不使用HttpMoudle,不用在每个按钮事件里加TraceSatae方法,不用每次页面请求时都判断
主要实现类
public static class RefreshTracker
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Constants#region Constants
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 刷新跟踪记数器
/// </summary>
public const string CurrentRefreshCounter = "CurrentRefreshCounter";
public const string LastRefreshCounter = "LastRefreshCounter";
public const string HiddenFieldRefreshCounter = "HiddenFieldRefreshtCounter";
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
private static int GetLastRefreshTicket(HttpContext ctx)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (ctx.Request.Cookies[RefreshTracker.LastRefreshCounter] == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HttpCookie cookie = new HttpCookie(LastRefreshCounter, "0");
ctx.Response.Cookies.Add(cookie);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return Convert.ToInt32(ctx.Request.Cookies[LastRefreshCounter].Value);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 从HiddenField中取出当前的刷新次数
/// </summary>
/// <param name="ctx"></param>
/// <returns></returns>
private static int GetCurrentRefreshTicket(HttpContext ctx)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (ctx.Request.Cookies[CurrentRefreshCounter] == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ctx.Response.Cookies.Add(new HttpCookie(CurrentRefreshCounter, "0"));
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return Convert.ToInt32(ctx.Request[HiddenFieldRefreshCounter]);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 更新最后刷新记数器
/// </summary>
/// <param name="ctx"></param>
/// <param name="ticket"></param>
private static void UpdateLastRefreshCounter(HttpContext ctx, int ticket)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (ctx.Request.Cookies[RefreshTracker.LastRefreshCounter] == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HttpCookie cookie = new HttpCookie(LastRefreshCounter, "0");
ctx.Response.Cookies.Add(cookie);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ctx.Response.Cookies[LastRefreshCounter].Value = ticket.ToString();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public static bool Check(HttpContext ctx)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//从Cookie中读出最后一次刷新记数
int lastTicket = GetLastRefreshTicket(ctx);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//从Cookie中读出当前的刷新记数
int thisTicket = GetCurrentRefreshTicket(ctx);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 如果当前刷新记数大于或等于最后次刷新记录数,说明是用户提交
if (thisTicket > lastTicket ||
(thisTicket == lastTicket && thisTicket == 0))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//更新最后一次刷新记数
UpdateLastRefreshCounter(ctx, thisTicket);
return false;
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return true;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 跟踪刷新,当用户点击后,将当前记数器加上1
/// </summary>
/// <param name="ctx"></param>
public static void TrackRefreshState(HttpContext ctx)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (ctx.Request.Cookies[CurrentRefreshCounter] == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ctx.Response.Cookies.Add(new HttpCookie(CurrentRefreshCounter, "0"));
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int ticket = Convert.ToInt32(ctx.Request.Cookies[CurrentRefreshCounter].Value) + 1;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ctx.Response.Cookies[CurrentRefreshCounter].Value = ticket.ToString();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 保存刷新记数到当前记数器和HiddenField
/// </summary>
public static void SaveRefreshState(Page page, HttpContext ctx)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (ctx.Request.Cookies[CurrentRefreshCounter] == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ctx.Response.Cookies.Add(new HttpCookie(CurrentRefreshCounter, "0"));
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int ticket = Convert.ToInt32(ctx.Request.Cookies[CurrentRefreshCounter].Value) + 1;
ctx.Response.Cookies[CurrentRefreshCounter].Value = ticket.ToString();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//将当前刷新数写入HiddenField,这样,当用户点刷新后,HiddenField中的值不会被加1
page.ClientScript.RegisterHiddenField(RefreshTracker.HiddenFieldRefreshCounter, ticket.ToString());
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
调用页面和示例代码
CS页
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
/// 判断当前的请求是否为用户刷新(如按F5)
/// </summary>
public bool IsPageRefresh
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return RefreshTracker.Check(this.Context);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected override void OnPreRender(EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
base.OnPreRender(e);
RefreshTracker.SaveRefreshState(this, this.Context);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected override void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
RefreshTracker.TrackRefreshState(this.Context);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
base.RaisePostBackEvent(sourceControl, eventArgument);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected void Button1_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
if (!this.IsPageRefresh)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write("Action");
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write("IsPageRefresh");
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
HTML页
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" ValidateRequest="false" %>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</head>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<body>
<form id="form1" runat="server">
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</form>
</body>
</html>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)