I have used jQuery Dialog to show the alert message. If you click on Refresh button of jQuery Dialog the session will be refreshes.
Execute it in a new website.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js" type="text/javascript"></script>
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/start/jquery-ui.css"
rel="stylesheet" type="text/css" />
<script type="text/javascript">
var interval;
var settimeout;
var newTimeout;
$(function () {
});
function SessionExpireAlert(timeout) {
var seconds = timeout / 1000;
clearInterval(interval);
interval = setInterval(function () {
seconds--;
document.getElementById("seconds").innerHTML = seconds;
}, 1000);
setTimeout(function () {
$("#dialog").dialog({
title: "View Details",
buttons: {
Ok: function () {
$(this).dialog('close');
},
Refresh: function () {
$.ajax({
type: "POST",
url: " Default.aspx/RefreshSession",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
newTimeout = r.d;
SessionExpireAlert(newTimeout);
},
error: function (r) {
alert(r.d);
}
});
clearTimeout(settimeout);
$(this).dialog('close');
}
},
modal: true
});
}, timeout - 5 * 1000);
settimeout = setTimeout(function () {
window.location = "Expired.aspx";
}, timeout);
}
</script>
</head>
<body>
<div id="dialog" style="display: none">
Your session will expire in 5 seconds. You will now be redirected. To restart the
session please click on Refresh Button
</div>
<form id="form1" runat="server">
<asp:Button ID="btnReset" Text="Reset" runat="server" OnClick="ResetSession" />
<br />
Your Session will expire in <span id="seconds"></span>seconds.
</form>
</body>
</html>
Namespaces
using System.Configuration;
using System.Web.Configuration;
C#
protected void Page_Load(object sender, EventArgs e)
{
Session["Name"] = "Mudassar";
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);
}
[System.Web.Services.WebMethod(EnableSession = true)]
public static int RefreshSession()
{
HttpContext.Current.Session["Name"] = "Mudassar";
Configuration config = WebConfigurationManager.OpenWebConfiguration("~/Web.Config");
SessionStateSection section = (SessionStateSection)config.GetSection("system.web/sessionState");
int timeout = (int)section.Timeout.TotalMinutes * 1000 * 60;
return timeout;
}
protected void ResetSession(object sender, EventArgs e)
{
string name = Session["Name"].ToString();
}