This way
HTML
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<asp:Button ID="btnReset" Text="Reset" runat="server" OnClientClick="return ResetSession()" />
<br />
Your Session will expire in <span id="seconds"></span> seconds.
<script type="text/javascript">
var interval;
function SessionExpireAlert(timeout) {
clearInterval(interval);
var seconds = timeout / 1000;
document.getElementById("seconds").innerHTML = seconds;
interval = setInterval(function () {
seconds--;
document.getElementById("seconds").innerHTML = seconds;
if (seconds == 0) {
window.location = "Expired.aspx";
}
}, 1000);
};
function ResetSession() {
PageMethods.ResetSession(OnSuccess);
return false;
}
function OnSuccess(response, userContext, methodName) {
SessionExpireAlert(response);
}
</script>
Namespaces
using System.Web.Services;
using System.Configuration;
using System.Web.Configuration;
Code
protected void Page_Load(object sender, EventArgs e)
{
Session["Reset"] = true;
int timeout = GetSessionTimeout();
ClientScript.RegisterStartupScript(this.GetType(), "SessionAlert", "SessionExpireAlert(" + timeout + ");", true);
}
[WebMethod(EnableSession = true)]
public static int ResetSession()
{
HttpContext.Current.Session["Reset"] = true;
int timeout = GetSessionTimeout();
return timeout;
}
private static int GetSessionTimeout()
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("~/Web.Config");
SessionStateSection section = (SessionStateSection)config.GetSection("system.web/sessionState");
return Convert.ToInt32(section.Timeout.TotalMinutes * 1000 * 60);
}
Web.Config
<sessionState timeout="1"/>