Skip to main content
All docs
V24.1

Refresh Data in a View After a Period of Time (ASP.NET Web Forms)

  • 3 minutes to read

This topic describes how to refresh data in a View after a period of time.

Create a WindowController

Add the following WindowController to the MySolution.Module.Web project:

File: MySolution.Module.Web\Controllers\AutoRefreshOnTimerMainWindowController.cs (AutoRefreshOnTimerMainWindowController.vb)

using System;  
using DevExpress.ExpressApp;  
using DevExpress.ExpressApp.Web;  
using DevExpress.ExpressApp.Web.Templates;  

namespace AutoRefreshSample.Module.Web.Controllers {  
    public class AutoRefreshOnTimerMainWindowController : WindowController, IXafCallbackHandler {
        public AutoRefreshOnTimerMainWindowController() {  
            TargetWindowType = WindowType.Main;  
        }  
        protected override void OnActivated() {  
            base.OnActivated();  
            Frame.ViewChanged += Frame_ViewChanged;  
            ((WebWindow)Window).PagePreRender += CurrentRequestWindow_PagePreRender;  
        }  
        protected override void OnDeactivated() {  
            ((WebWindow)Window).PagePreRender -= CurrentRequestWindow_PagePreRender;  
            Frame.ViewChanged -= Frame_ViewChanged;  
            base.OnDeactivated();  
        }  
        void Frame_ViewChanged(object sender, ViewChangedEventArgs e) {  
            if (Frame.View != null) {  
                Frame.View.ControlsCreated += View_ControlsCreated;  
            }  
        }  
        void View_ControlsCreated(object sender, EventArgs e) {  
            RegisterXafCallackHandler();  
        }  
        private void RegisterXafCallackHandler() {  
            if (XafCallbackManager != null) {  
                XafCallbackManager.RegisterHandler("TimerCallback", this);  
            }  
        }  
        protected XafCallbackManager XafCallbackManager {  
            get {  
                return WebWindow.CurrentRequestPage != null ? ((ICallbackManagerHolder)WebWindow.CurrentRequestPage).CallbackManager : null;  
            }  
        }  
        void CurrentRequestWindow_PagePreRender(object sender, EventArgs e) {  
            WebWindow window = (WebWindow)sender;  
            string script = IsSuitableView() ? "window.startXafViewRefreshTimer(3000);" : "window.stopXafViewRefreshTimer();";  
            window.RegisterStartupScript(GetType().FullName, script);  
        }  
        // XAF calls this method every time an XAF callback is raised by the timer.
        public void ProcessAction(string parameter) {  
            if (IsSuitableView() && (Frame.View != null)) {  
                Frame.View.ObjectSpace.Refresh();  
            }  
        }  
        // Modify this method to enable the timer for required Views only.  
        protected virtual bool IsSuitableView() {  
            return Frame.View != null && Frame.View.IsRoot && !(Frame.View is DetailView) && !(Frame is NestedFrame) && !(Frame is PopupWindow);  
        }

    }  
}  

Modify Default.aspx

Modify the Default.aspx file in the MySolution.Web project.

File: MySolution.Web\Controllers\Default.aspx

<head runat="server">  
    <title>Main Page</title>  
    <meta http-equiv="Expires" content="0" />  
<script type="text/javascript">  
        window.xafViewRefreshTimer = null;  
        window.startXafViewRefreshTimer = function (delay) {  
            if (!window.xafViewRefreshTimer) {  
                window.xafViewRefreshTimer = window.setInterval(onXafViewRefreshTimer, delay);  
            }  
        }  
        window.stopXafViewRefreshTimer = function () {  
            if (window.xafViewRefreshTimer) {  
                window.clearInterval(window.xafViewRefreshTimer);  
                window.xafViewRefreshTimer = null;  
            }  
        }  
        window.isValidXafView = function () {  
            return !GetActivePopupControl(window.parent) || !GetActivePopupControl(window);  
        }  
        window.onXafViewRefreshTimer = function () {  
            if (isValidXafView()) {  
                RaiseXafCallback(globalCallbackControl, 'TimerCallback', '', '', false);  
            }  
        }  
    </script>  
 </head>  
See Also