Check this example it works fine for me in Visual Studio.
HTML
<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.
<script type="text/javascript">
function SessionExpireAlert(timeout) {
var seconds = timeout / 1000;
seconds--;
document.getElementById("seconds").innerHTML = seconds;
setInterval(function () {
seconds--;
document.getElementById("seconds").innerHTML = seconds;
}, 1000);
setTimeout(function () {
alert("Your session has expired. You will now be redirected.");
window.location = "Expired.aspx";
}, timeout);
}
</script>
</form>
Namespaces
using System.Configuration;
using System.Web.Configuration;
Code
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);
}
protected void ResetSession(object sender, EventArgs e)
{
string name = Session["Name"].ToString();
}
Web.Config
<sessionState timeout="1"/>