Click here to Skip to main content
13,899,227 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


22 bookmarked
Posted 12 Jul 2013
Licenced CPOL

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

, 12 Jul 2013
Rate this:
Please Sign up or sign in to vote.
Learn how to add a friendly 'Session Timeout' warning dialog message to your ASP.NET MVC website using the DevExpress MVC PopupControl extension

Editorial Note

This article is for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers


Learn how to add a friendly 'Session Timeout' warning dialog message to your ASP.NET MVC website using the DevExpress ASP.NET MVC PopupControl extension. This helps you 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 our automatically

ASP.NET MVC Session Timeout Popup Warning - DevExpress

This sample is the ASP.NET MVC version of the previous sample I mentioned here.

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 of the best way to notify your end-users 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.

Solution: MVC PopupControl Extension

Now you can have this wonderful feature in your websites using the DevExpress ASP.NET MVC PopupControl extension!

Download the sample ASP.NET MVC solution here:



Project Details

Let's break down what's in the project. The slick sample contains 4 key files:

  • TimeoutPartial.cshtml - Partial view that contains the Popup warning dialog
  • TimeOutPage.cshtml - Simple page to redirect to when session has timed out
  • Index.cshtml - Default page that calls the TimeoutPartial view
  • HomeController.cs - Controller containing the action methods for routing to TimeOutPartial view

All the Java‍Script magic happens in the TimeoutPartial.cshtml view:

@functions {

    public int PopupShowDelay {
        get { return 60000 * (Session.Timeout - 1); }


<script type="text/javascript">
    window.SessionTimeout = (function() {
        var _timeLeft, _popupTimer, _countDownTimer;

        var stopTimers = function() {

        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) {
                _countDownTimer = window.setTimeout(updateCountDown, 1000);
            } else  {
                window.location = "Home/TimeOutPage";

        var showPopup = function() {
            _timeLeft = 60;

        var schedulePopup = function() {
            _popupTimer = window.setTimeout(showPopup, @PopupShowDelay);

        var sendKeepAlive = function() {

        return {
            schedulePopup: schedulePopup,
            sendKeepAlive: sendKeepAlive



@using (Html.BeginForm()) {
        A timeout warning popup will be shown every @(Session.Timeout - 1) min.

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.


Build Your Best - Without Limits or Compromise

Try the DevExpress ASP.NET MVC Extensions online now:

Read the latest news about DevExpress ASP.NET MVC Extensions:

Download a free and fully-functional version of DXperience now:


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mehul Harry
Web Developer
United States United States
.Net, C#, SQL, Delphi

You may also be interested in...

Comments and Discussions

QuestionProject not opening? Pin
Shemeemsha (ഷെമീംഷ)15-Sep-14 20:30
memberShemeemsha (ഷെമീംഷ)15-Sep-14 20:30 
QuestionSession restore Pin
Member 1011572016-Jul-14 14:41
memberMember 1011572016-Jul-14 14:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190306.1 | Last Updated 12 Jul 2013
Article Copyright 2013 by Mehul Harry
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid