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.
For more information, refer to the following blog posts:
Files to Look At
- Default.aspx (VB: Default.aspx)
- Default.aspx.cs (VB: Default.aspx.vb)
- TimeoutControl.ascx (VB: TimeoutControl.ascx)
- TimeoutControl.ascx.cs (VB: TimeoutControl.ascx.vb)
- TimeOutPage.aspx (VB: TimeOutPage.aspx)
Documentation
More Examples
- Popup Control for ASP.NET Web Forms - How to show a pop-up window
- Popup Control for ASP.NET Web Forms - How to add buttons to a pop-up window
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
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>
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>
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("\"", "\\\"") + '"'; }
}
}
}
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>