TrapRefreshPage改进版

捕捉刷新事件
参考了以下文章
http://msdn2.microsoft.com/en-us/library/ms379557(VS.80).aspx

做了一些改进,把计数器放到Cookie中,不使用HttpMoudle,不用在每个按钮事件里加TraceSatae方法,不用每次页面请求时都判断

主要实现类

     public   static   class  RefreshTracker
    
{
        
Constants

       

        
private static int GetLastRefreshTicket(HttpContext ctx)
        
{
            
if (ctx.Request.Cookies[RefreshTracker.LastRefreshCounter] == null)
            
{
                HttpCookie cookie 
= new HttpCookie(LastRefreshCounter, "0");
                ctx.Response.Cookies.Add(cookie);
            }


            
return Convert.ToInt32(ctx.Request.Cookies[LastRefreshCounter].Value);
        }

        
        
/// <summary>
        
/// 从HiddenField中取出当前的刷新次数
        
/// </summary>
        
/// <param name="ctx"></param>
        
/// <returns></returns>

        private static int GetCurrentRefreshTicket(HttpContext ctx)
        
{
            
if (ctx.Request.Cookies[CurrentRefreshCounter] == null)
            
{
                ctx.Response.Cookies.Add(
new HttpCookie(CurrentRefreshCounter, "0"));
            }


            
return Convert.ToInt32(ctx.Request[HiddenFieldRefreshCounter]);
        }

        
/// <summary>
        
/// 更新最后刷新记数器
        
/// </summary>
        
/// <param name="ctx"></param>
        
/// <param name="ticket"></param>

        private static void UpdateLastRefreshCounter(HttpContext ctx, int ticket)
        
{
            
if (ctx.Request.Cookies[RefreshTracker.LastRefreshCounter] == null)
            
{
                HttpCookie cookie 
= new HttpCookie(LastRefreshCounter, "0");
                ctx.Response.Cookies.Add(cookie);
            }


            ctx.Response.Cookies[LastRefreshCounter].Value 
= ticket.ToString();
        }


        
public static bool Check(HttpContext ctx)
        
{


            
//从Cookie中读出最后一次刷新记数
            int lastTicket = GetLastRefreshTicket(ctx);

            
//从Cookie中读出当前的刷新记数
            int thisTicket = GetCurrentRefreshTicket(ctx);


            
// 如果当前刷新记数大于或等于最后次刷新记录数,说明是用户提交
            if (thisTicket > lastTicket ||
                (thisTicket 
== lastTicket && thisTicket == 0))
            
{
                
//更新最后一次刷新记数
                UpdateLastRefreshCounter(ctx, thisTicket);
                
return false;
            }

            
else
            
{
                
return true;
            }

        }




        
/// <summary>
        
/// 跟踪刷新,当用户点击后,将当前记数器加上1
        
/// </summary>
        
/// <param name="ctx"></param>

        public static void TrackRefreshState(HttpContext ctx)
        
{
            
if (ctx.Request.Cookies[CurrentRefreshCounter] == null)
            
{
                ctx.Response.Cookies.Add(
new HttpCookie(CurrentRefreshCounter, "0"));
            }


            
int ticket = Convert.ToInt32(ctx.Request.Cookies[CurrentRefreshCounter].Value) + 1;


            ctx.Response.Cookies[CurrentRefreshCounter].Value 
= ticket.ToString();


        }


        
/// <summary>
        
/// 保存刷新记数到当前记数器和HiddenField
        
/// </summary>

        public static void SaveRefreshState(Page page, HttpContext ctx)
        
{
            
if (ctx.Request.Cookies[CurrentRefreshCounter] == null)
            
{
                ctx.Response.Cookies.Add(
new HttpCookie(CurrentRefreshCounter, "0"));
            }


            
int ticket = Convert.ToInt32(ctx.Request.Cookies[CurrentRefreshCounter].Value) + 1;
            ctx.Response.Cookies[CurrentRefreshCounter].Value 
= ticket.ToString();

            
//将当前刷新数写入HiddenField,这样,当用户点刷新后,HiddenField中的值不会被加1
            page.ClientScript.RegisterHiddenField(RefreshTracker.HiddenFieldRefreshCounter, ticket.ToString());
        }

     

    }
调用页面和示例代码
CS页
     /// <summary>
    
/// 判断当前的请求是否为用户刷新(如按F5)
    
/// </summary>

     public   bool  IsPageRefresh
    
{
        
get
        
{
            
return RefreshTracker.Check(this.Context);
        }

    }



    
protected   override   void  OnPreRender(EventArgs e)
    
{

        
base.OnPreRender(e);
        RefreshTracker.SaveRefreshState(
thisthis.Context);

    }


    
protected   override   void  RaisePostBackEvent(IPostBackEventHandler sourceControl,  string  eventArgument)
    
{
        RefreshTracker.TrackRefreshState(
this.Context);

        
base.RaisePostBackEvent(sourceControl, eventArgument);
    }


    
protected   void  Button1_Click( object  sender, EventArgs e)
    
{
        
if (!this.IsPageRefresh)
        
{
            Response.Write(
"Action");
        }

        
else
        
{
            Response.Write(
"IsPageRefresh");
        }

    }

HTML页
<% @ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" ValidateRequest="false"  %>


<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< html  xmlns ="http://www.w3.org/1999/xhtml"   >
< head  runat ="server" >
    
< title > 无标题页 </ title >

    
</ head >

< body >
    
< form  id ="form1"  runat ="server" >


    
< asp:Button  ID ="Button1"  runat ="server"  Text ="Button"  OnClick ="Button1_Click"   />


    
</ form >
</ body >
</ html >

你可能感兴趣的:(refresh)