Click here to Skip to main content
15,893,923 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.2K   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
{
    public class Module : IHttpModule, IRequiresSessionState
    {
        public void Init(HttpApplication context)
        {
            context.AcquireRequestState += OnAcquireRequestState;
            context.EndRequest += OnEndRequest;
            context.PreRequestHandlerExecute += OnPreRequestHandlerExecute;
        }
        
        private void OnPreRequestHandlerExecute(object sender, EventArgs e)
        {

            
            if (ContextHelper.IsRequestPage && 
                RefreshManager.IsPageRefreshed && 
                ContextHelper.IsCustomModuleExist)
            {
                HttpContext.Current.Response.Clear();
                RefreshUIManager uiManager = new RefreshUIManager(ContextHelper.RefreshAttribute.MessageModulePath);
                HttpContext.Current.Response.Write(uiManager.RenderContent(ContextHelper.Handler));
                HttpContext.Current.Response.End();
            }
        }
       
        
        private void OnEndRequest(object sender, EventArgs e)
        {
            if (ContextHelper.IsRequestPage &&
               !ContextHelper.IsRequestMethodGET &&
                    ContextHelper.IsAutoRedirect)
                        HttpContext.Current.Response.Redirect(ContextHelper.Context.Request.RawUrl, true);
            
        }

        private void OnAcquireRequestState(object sender, EventArgs e)
        {
            if (ContextHelper.IsRequestPage && 
                ContextHelper.IsRefreshAttributeDefiened && 
                ContextHelper.Context.CurrentHandler is Page)
            {   
                ContextHelper.Handler.PreRenderComplete += OnPreRenderComplete;
                if (!ContextHelper.IsRequestMethodGET)
                {
                    RefreshManager manager = new RefreshManager();
                    manager.CheckRequest();
                }   
            } 
        }

        private void OnPreRenderComplete(object sender, EventArgs e)
        {
            RefreshManager manager = new RefreshManager();
            manager.RegisterHidenFields(sender as Page);   
        }

        public void Dispose()
        {
            //Do nothing here for now
        }
    }
}

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