Click here to Skip to main content
15,888,984 members
Articles / Web Development / ASP.NET

Refresh Module

Rate me:
Please Sign up or sign in to vote.
4.68/5 (11 votes)
29 May 20075 min read 69.1K   1.1K   39  
How to make a browser's refresh manageable
using System;
using System.IO;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;

namespace RefreshModule
{
    internal class RefreshManager
    {   
        private class ClientInfo
        {
            public string PageID;
            public int Ticket;
            public int Hash;
            public ClientInfo(string pageID, int ticket, int hash)
            {
                PageID = pageID;
                Ticket = ticket;
                Hash = hash;
            }
        }
            
        private const string INFO_ON_CLIENT_SIDE = "_iocs_";
        private const string TICKET_MANAGER_ID = "_tmi_";
        private const string REFRESH_ENTRY = "_re_";
        public const string HIDDENFIELD_TEMPLATE = "[{0}]:[{1}]";
        private HttpContext context = null;

        public RefreshManager():this(HttpContext.Current)
        {
            
        }
        
        public RefreshManager(HttpContext context)
        {
            this.context = context;
            InitializeClientData();
        }
        
        private void InitializeClientData()
        {
            if (context.Items[INFO_ON_CLIENT_SIDE] == null)
            {
                TextReader read = new StreamReader(context.Request.InputStream);
                string input = read.ReadToEnd();
                try
                {
                    string[] ci = context.Request[INFO_ON_CLIENT_SIDE].Split(":".ToCharArray());
                    
                    Guid pageID = new Guid(ci[0].Trim("[]".ToCharArray()));
                    int ticket = Int32.Parse(ci[1].Trim("[]".ToCharArray()));
                    context.Items[INFO_ON_CLIENT_SIDE] =
                        new ClientInfo(pageID.ToString(),ticket, input.GetHashCode());
                }
                catch
                {

                    context.Items[INFO_ON_CLIENT_SIDE] =
                        new ClientInfo(Guid.NewGuid().ToString(), 0, input.GetHashCode());
                }
            }
        }
        
        public string PageID
        {
            get
            {
                return (context.Items[INFO_ON_CLIENT_SIDE] as ClientInfo).PageID;
            }
        }

        public int ClientTicket
        {   
            get
            {
                return (context.Items[INFO_ON_CLIENT_SIDE] as ClientInfo).Ticket;
            }
        }


        public int CurrentHash
        {
            get
            {
                return (context.Items[INFO_ON_CLIENT_SIDE] as ClientInfo).Hash;
            }
        }
        
        public static bool IsPageRefreshed
        {
            get
            {
                if(HttpContext.Current.Items[REFRESH_ENTRY] == null)
                    return false;
                return (bool) HttpContext.Current.Items[REFRESH_ENTRY]; 
            }
            
            internal set
            {
                HttpContext.Current.Items[REFRESH_ENTRY] = value; 
            }
        }
        
        public void RegisterHidenFields(Page page)
        {
            TicketManager manager = Manager;
            if (!manager.IsPageRegistred(PageID))
            {
                manager.RegisterKey(PageID, CurrentHash);
            }
            page.ClientScript.RegisterHiddenField(INFO_ON_CLIENT_SIDE,
                string.Format(HIDDENFIELD_TEMPLATE, PageID, manager.GetNextTicket(PageID, IsPageRefreshed)));
        }
       
      

        public TicketManager Manager
        {
            get
            {
                 HttpSessionState session = HttpContext.Current.Session;
                 if (session[TICKET_MANAGER_ID] == null)
                    session[TICKET_MANAGER_ID] = new TicketManager();
                
                 return session[TICKET_MANAGER_ID] as TicketManager;
            }
        }
      
        public bool CheckRequest()
        {
            TicketManager manager = Manager;
            
            if(!manager.IsPageRegistred(PageID))
            {
                manager.RegisterKey(PageID, CurrentHash);
            }

            IsPageRefreshed = manager.IsTicketCashed(
                PageID, ClientTicket, CurrentHash);
            
            if(!IsPageRefreshed)
                manager.UpdateTicket(PageID, ClientTicket, CurrentHash);
            
            return IsPageRefreshed;
        }
   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Belarus Belarus
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions