Example E3302
Visible to All Users

Popup Control for ASP.NET Web Forms - How to control state when the Session is being expired and prolong it on demand

This example shows how to display a warning message before a user's session times out, and how to either allow the user to continue the session or log them out automatically.

Results

For more information, refer to the following blog posts:

Files to Look At

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

WebApp/Default.aspx
ASPx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApp._Default" %> <%@ Register Src="TimeoutControl.ascx" TagName="TimeoutControl" TagPrefix="uc1" %> <!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"> <p> A timeout warning popup will be shown every <%= Session.Timeout - 1 %> min. </p> <uc1:TimeoutControl ID="TimeoutControl1" runat="server" /> </form> </body> </html>
WebApp/Default.aspx.cs(vb)
C#
using System; namespace WebApp { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { TimeoutControl1.TimeOutUrl = "~/TimeOutPage.aspx"; } } }
WebApp/TimeoutControl.ascx
Code
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TimeoutControl.ascx.cs" Inherits="WebApp.TimeoutControl" %> <%@ Register Assembly="DevExpress.Web.v24.2, Version=24.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web" TagPrefix="dx" %> <script type="text/javascript"> window.SessionTimeout = (function() { var _timeLeft, _popupTimer, _countDownTimer; var stopTimers = function() { window.clearTimeout(_popupTimer); window.clearTimeout(_countDownTimer); }; var updateCountDown = function() { var min = Math.floor(_timeLeft / 60); var sec = _timeLeft % 60; if(sec < 10) sec = "0" + sec; document.getElementById("CountDownHolder").innerHTML = min + ":" + sec; if(_timeLeft > 0) { _timeLeft--; _countDownTimer = window.setTimeout(updateCountDown, 1000); } else { window.location = <%= QuotedTimeOutUrl %>; } }; var showPopup = function() { _timeLeft = 60; updateCountDown(); ClientTimeoutPopup.Show(); }; var schedulePopup = function() { stopTimers(); _popupTimer = window.setTimeout(showPopup, <%= PopupShowDelay %>); }; var sendKeepAlive = function() { stopTimers(); ClientTimeoutPopup.Hide(); ClientKeepAliveHelper.PerformCallback(); }; return { schedulePopup: schedulePopup, sendKeepAlive: sendKeepAlive }; })(); </script> <dx:ASPxPopupControl runat="server" ID="TimeoutPopup" ClientInstanceName="ClientTimeoutPopup" CloseAction="None" HeaderText="Session Expiring" Modal="True" PopupHorizontalAlign="WindowCenter" PopupVerticalAlign="WindowCenter" ShowCloseButton="False" Width="250px" ShowFooter="true" AllowDragging="true"> <ContentCollection> <dx:PopupControlContentControl ID="PopupControlContentControl1" runat="server" SupportsDisabledAttribute="True"> Your session is about to expire! <br /> <br /> <span id="CountDownHolder"></span> <br /> <br /> Click OK to continue your session. </dx:PopupControlContentControl> </ContentCollection> <FooterTemplate> <dx:ASPxButton runat="server" ID="OkButton" Text="OK" AutoPostBack="false"> <ClientSideEvents Click="SessionTimeout.sendKeepAlive" /> </dx:ASPxButton> </FooterTemplate> <FooterStyle> <Paddings Padding="5" /> </FooterStyle> </dx:ASPxPopupControl> <dx:ASPxGlobalEvents runat="server" ID="GlobalEvents"> <ClientSideEvents ControlsInitialized="SessionTimeout.schedulePopup" /> </dx:ASPxGlobalEvents> <dx:ASPxCallback runat="server" ID="KeepAliveHelper" ClientInstanceName="ClientKeepAliveHelper"> </dx:ASPxCallback>
WebApp/TimeoutControl.ascx.cs(vb)
C#
using System; namespace WebApp { public partial class TimeoutControl : System.Web.UI.UserControl { public string TimeOutUrl = ""; public int PopupShowDelay { get { return 60000 * (Session.Timeout - 1); } } protected string QuotedTimeOutUrl { get { return '"' + ResolveClientUrl(TimeOutUrl).Replace("\"", "\\\"") + '"'; } } } }
WebApp/TimeOutPage.aspx
ASPx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TimeOutPage.aspx.cs" Inherits="WebApp.TimeOutPage" %> <!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"> <p> Sorry, your Session has expired. </p> </form> </body> </html>

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.