Blogs

News

Email Subscriptions

Mehul Harry's DevExpress Blog

ASP.NET - How To Show A Popup Warning Before Session Timeout

     

Check out this useful Code Central ASP.NET sample that shows you how to use the DevExpress ASP.NET PopupControl to:

  1. Display a warning message before a user's session times out and
  2. Allow the end-user to continue the session or log them out automatically

E3302

Session Timeout

Session Timeout is a property that you can set in your web.config file to control when a user session should expire.

Unfortunately, your end-users don't know when their session will expire unless you notify them somehow.

Popup Warning

One one of the best way to notify them is using a popup warning dialog. You may have seen these types of popups on your bank's website.

Say you're checking your bank balance online and need to answer a phone call elsewhere. You've just left the browser open at your bank account details which creates a potential security issue. To put it mildly.

If the ASP.NET Session timeout has been set then it will expire the session but it may not give any useful hints to the end-user. Most banking websites will display a client-side popup dialog to warn and ask the end-users if they would like to continue the session.

The popup could have a one minute timer and an OK button. If the end-user is still there and wants to continue the session then they'll click the 'Ok' button. But if the timer on the popup runs out then they'll automatically be logged out.

ASPxPopupControl To The Rescue

Now you can have this wonderful feature in your websites using the ASPxPopupControl! We've put together a Code Central sample that contains a ready-to-use UserControl that has our ASPxPopupControl embedded within it:

How to control state when the Session is being expired and prolong it on demand

To learn how Code Central works, watch this short video.

Note: You can't run this demo online, even though it's an ASP.NET project, because it uses the session state. However, you can easily download and run it locally.

Break It Down

The slick Code Central sample contains 4 files:

  • TimeoutControl.ascx - User Control that contains the Popup warning dialog
  • TimeOutPage.aspx - Simple page to redirect to when session has timed out
  • Default.aspx - Default page that calls the UserControl
  • Default.aspx.cs - Code behind page that sets the TimeOutUrl to point to TimeOutPage.aspx

All the Java Script magic happens in the Timeoutcontrol.ascx UserControl:

<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  {
                document.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>

Download the sample and integrate into your websites to give your end-users a friendly reminder that their session is expiring soon and would they like to keep it running. Smile

Thanks!

DXperience? What's That?

DXperience is the .NET developer's secret weapon. Get full access to a complete suite of professional components that let you instantly drop in new features, designer styles and fast performance for your applications. Try a fully-functional version of DXperience for free now: http://www.devexpress.com/Downloads/NET/

Follow MehulHarry on Twitter

Published Jun 15 2011, 12:45 PM by Mehul Harry (DevExpress)
Technorati tags: How-To, ASP.NET, ASPxPopupControl
Bookmark and Share

Comments

 

Jens Havelberg said:

in XAF integrated?

June 15, 2011 4:11 PM
 

Mehul Harry (DevExpress) said:

Jens,

This is a code central sample so I don't believe it's integrated directly into XAF. I've asked the XAF team to look into it for you and they'll comment soon about it.

Thanks.

June 15, 2011 4:30 PM
 

Brendon Muck [DX Squad] said:

Very helpful. Thanks, Mehul!

June 15, 2011 4:31 PM
 

Dennis (DevExpress Support) said:

As Mehul mentioned above, it is not integrated in XAF. Furthermore, in XAF we have our own mechanism that automatically prolongs the session. If you are interested to learn more about it, please notice the SessionKeepAliveReconnect.aspx page in the root folder of your website. And if you have access to XAF sources, check out the SessionKeepAliveReconnect method in the CommonFunctions.js file as well as the use of the SessionKeepAliveControl class.

However, you can implement the solution demonstrated in this blog in XAF Web applications because XAF produces regular ASP.NET applications where the same customization techniques can be used.

June 16, 2011 4:56 AM
 

Fırat Esmer said:

I liked it :)

June 17, 2011 7:35 AM
 

Jean Pierre RAVEZ said:

This solution assumes that each user will use only one client window at a time on the web application , no ? Do you think it's possible to implement a "simple" solution that would work with several windows opened at the same time (sharing the same session) by a single user ? Thanks !

June 21, 2011 3:58 AM
 

Mehul Harry (DevExpress) said:

Jean,

The approach from the example will prolong the session for all browser windows, but other windows cannot know about it and will be redirected to the timeout page, so the user will have to use the “Back” button to continue their work.

Unfortunately, there isn't a simple solution here. To solve it, there should be some kind of opened window tracking and cross-window communication. And this cannot be simple and universal, sorry.

June 21, 2011 4:41 AM
 

Yves Lessard said:

In a Master Page project ... would it be wyse to instantiate the TimeoutControl.ascx in the master page ?

June 21, 2011 8:47 AM
 

Huu Huyen 1 said:

Is there equivalent code as Razor partial page? And can this be incorporated in common view layout page (similar to above question into master page).

June 21, 2011 12:26 PM
 

Mehul Harry (DevExpress) said:

Yves,

Yes, you can put the usercontrol on the master page. :)

Thanks.

June 21, 2011 3:33 PM
 

Mehul Harry (DevExpress) said:

Huu,

There isn't an MVC version of this. I'll look into it and get back to you.

June 21, 2011 3:36 PM
 

Mehul Harry (DevExpress) said:

Huu,

Here a version of this sample for ASP.NET MVC that uses DevExpress MVC Popupcontrol extension: http://dxpr.es/pNGAJ3

July 11, 2011 6:19 PM
 

Huu Huyen said:

Mehul, thanks for providing example in MVC. I tried to incorporate your example into my project but the popup control does not display correctly. My DevExpress version is 10.2 and your example is DevExpress version 11.1. Do I need to upgrade DevExpress version again? It looks like Popup Control support has changed.

Also can I incorporate this feature into _Layout.cshtml so it will apply to all pages and only render TimeoutPartial when request is authenticated as it only makes sense to show timeout if user has logged in.

August 12, 2011 3:23 PM
 

Mehul Harry (DevExpress) said:

Huu,

No problem and yes, I recommend upgrading to the latest version because we've added some great improvements that you'll want to take advantage of:

community.devexpress.com/.../asp-net-popup-control-lightweight-rendering-scroll-bars-css3-and-more-coming-soon-in-v2011-vol-1.aspx

Thanks.

August 12, 2011 3:33 PM
 

sagar metkar 1 said:

this Code is good.

but when i use UserControl  popup control and timeout popup is display same time, if i want session live then i click ok button then previous popup control client side validation create static to increment session how can solve this problem..

March 1, 2012 6:45 AM
 

Mehul Harry (DevExpress) said:

Sagar,

Thanks, I recommend that you contact our support team and they can help you:

www.devexpress.com/.../CreateIssue.aspx

March 1, 2012 12:55 PM

About Mehul Harry (DevExpress)

Mehul Harry is an ASP.NET technical evangelist at Developer Express. You can reach him directly at mharry@DevExpress.com. You can also follow him on Twitter: http://twitter.com/mehulharry
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.