I discovered an improved solution that I merged with the initial one. Below is the revised JS code:
var interval;
function SessionExpireAlert(timeout) {
clearInterval(interval);
var seconds = timeout / 1000;
document.getElementById("seconds").innerHTML = seconds;
document.getElementById("secondsIdle").innerHTML = seconds;
interval = setInterval(function () {
seconds--;
document.getElementById("seconds").innerHTML = seconds;
document.getElementById("secondsIdle").innerHTML = seconds;
}, 1000);
setTimeout(function () {
//Display Popup before 50 seconds of timeout.
$find("mpeTimeout").show();
}, timeout - 50 * 1000);
setTimeout(function () {
window.location = "Login.aspx";
}, timeout);
};
function ResetSession() {
PageMethods.ResetSession(OnSuccess);
return false;
}
function OnSuccess(response, userContext, methodName) {
SessionExpireAlert(response);
}
Here's the ASPX code snippet:
<%-- FOR TIME OUT --%>
<script type="text/javascript" src="js/timeOutScript.js"></script>
<h3>Session Idle: <span id="secondsIdle"></span> seconds.</h3>
<asp:LinkButton ID="lnkFake3" runat="server" />
<ajax:ModalPopupExtender ID="mpeTimeout" BehaviorID ="mpeTimeout" runat="server" PopupControlID="pnlPopupmpeTimeout" TargetControlID="lnkFake3"
OkControlID="btnYes" CancelControlID="btnNo" BackgroundCssClass="modalBackground3" OnOkScript = "ResetSession()">
</ajax:ModalPopupExtender>
<asp:Panel ID="pnlPopupmpeTimeout" runat="server" CssClass="modalPopup3" Style="display: none">
<div class="header">
Session Expiring!
</div>
<div class="body">
Your Session will expire in <span id="seconds"></span> seconds.<br />
Do you want to continue?
</div>
<div class="footer" align="right">
<asp:Button ID="btnYes" runat="server" Text="Yes" CssClass="yes3" OnClick="btnYes_Click" OnClientClick="return ResetSession()"/>
<asp:Button ID="btnNo" runat="server" Text="No" CssClass="no3" OnClick="btnNo_Click"/>
</div>
</asp:Panel>
<%-- ENDS TIME OUT --%gt;
Lastly, here's the C# backend code:
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
lbUserID.Text = Context.User.Identity.Name;
UpdateProgress1.AssociatedUpdatePanelID = UpdatePanel1.UniqueID;
lbLocalTime.Text = DateTime.Now.ToLongDateString();
if (!this.IsPostBack)
{
/*FOR TIMEOUT PURPOSE*/
Session["Reset"] = true;
Configuration config = WebConfigurationManager.OpenWebConfiguration("~/Web.Config");
SessionStateSection section = (SessionStateSection)config.GetSection("system.web/sessionState");
int timeout = GetSessionTimeout();
ClientScript.RegisterStartupScript(this.GetType(), "SessionAlert", "SessionExpireAlert(" + timeout + ");", true);
/*ENDS TIME OUT*/
}
}
[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);
}
Error Tag: 0x800a1391 – There was a JavaScript runtime error: ‘PageMethods’ is undefined.