Display Session Timeout message before Session expires in ASP.Net C#

In this article, I will show to session out values in website like payment gateway page.  Many times we need to show session values in our project so this code will be help you
Web.config:-
    <!--for one minute-->
   <sessionState timeout = "1"/>


Source Code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="sessiontimeout.aspx.cs" Inherits="sessiontimeout" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function SessionExpireAlert(timeout) {
            var seconds = timeout / 1000;
            document.getElementsByName("secondsIdle").innerHTML = seconds;
            document.getElementsByName("seconds").innerHTML = seconds;
            setInterval(function () {
                seconds--;
                document.getElementById("seconds").innerHTML = seconds;
                document.getElementById("secondsIdle").innerHTML = seconds;
            }, 1000);
        
            setTimeout(function () {
                window.location = "http://www.neerajcodesolutions.com/";
            }, timeout);
        };
        function ResetSession() {
            //Redirect to refresh Session.
            window.location = window.location.href;
        }
</script>
</head>
<body>
    <form id="form1" runat="server">
    Your Session will expire in&nbsp;<span id="seconds"></span>&nbsp;seconds.
    </form>
</body>
</html>

Code behind (c#):-
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class sessiontimeout : System.Web.UI.Page

{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        if (!this.IsPostBack)
        {
            Session["Reset"] = true;
            Configuration config = WebConfigurationManager.OpenWebConfiguration("~/Web.Config");
            SessionStateSection section = (SessionStateSection)config.GetSection("system.web/sessionState");
            int timeout = (int)section.Timeout.TotalMinutes * 1000 * 60;
            ClientScript.RegisterStartupScript(this.GetType(), "SessionAlert", "SessionExpireAlert(" + timeout + ");", true);
        }
    }
}

OUT-PUT:-



Display Session Timeout message before Session expires in ASP.Net C# Display Session Timeout message before Session expires in ASP.Net C# Reviewed by NEERAJ SRIVASTAVA on 5:25:00 PM Rating: 5

1 comment:


  1. the blog is very interesting and will be much useful for us. thank you for sharing the blog with us. please keep on updating.
    SEO Company in Chennai

    ReplyDelete

Powered by Blogger.