Please refer below code.
HTML
<div>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<h3>
Session Idle: <span id="secondsIdle"></span> seconds.</h3>
<asp:LinkButton ID="lnkFake" runat="server" />
<asp:ModalPopupExtender ID="mpeTimeout" BehaviorID="mpeTimeout" runat="server" PopupControlID="pnlPopup"
TargetControlID="lnkFake" OkControlID="btnYes" CancelControlID="btnNo" BackgroundCssClass="modalBackground"
OnOkScript="ResetSession()">
</asp:ModalPopupExtender>
<asp:Panel ID="pnlPopup" runat="server" CssClass="modalPopup" 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 reset?
</div>
<div class="footer" align="right">
<asp:Button ID="btnYes" runat="server" Text="Yes" CssClass="yes" />
<asp:Button ID="btnNo" runat="server" Text="No" CssClass="no" />
</div>
</asp:Panel>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
var IntialInterval;
var IntialTimeOut;
var IntialTimeOutExpiry;
$(function () {
SetSessionExpiry();
$('[id*=form1]').on('click', function () {
SetSessionExpiry();
});
$('[id*=btnNo]').on('click', function () {
$('[id*=form1]').unbind('click');
});
});
function SetSessionExpiry() {
$.ajax({
type: "POST",
url: "CS.aspx/SetSessionExpiry",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var timeout = r.d;
if (timeout != null) {
SessionExpireAlert(timeout);
}
},
error: function (r) {
alert(r.responseText);
},
failure: function (r) {
alert(r.responseText);
}
});
return false;
}
function SessionExpireAlert(timeout) {
timeout = timeout / 2; // Remove this line,I have set it to 30 seconds
clearInterval(IntialInterval);
clearTimeout(IntialTimeOut);
clearTimeout(IntialTimeOutExpiry);
var seconds = (timeout) / 1000;
document.getElementsByName("secondsIdle").innerHTML = seconds;
document.getElementsByName("seconds").innerHTML = seconds;
IntialInterval = setInterval(function () {
seconds--;
document.getElementById("seconds").innerHTML = seconds;
document.getElementById("secondsIdle").innerHTML = seconds;
}, 1000);
IntialTimeOut = setTimeout(function () {
//Show Popup before 20 seconds of timeout.
$find("mpeTimeout").show();
}, timeout - (20 * 1000));
IntialTimeOutExpiry = setTimeout(function () {
window.location = "Expired.aspx";
}, timeout);
};
function ResetSession() {
//Redirect to refresh Session.
window.location = window.location.href;
}
</script>
</div>
Code
[WebMethod]
public static int SetSessionExpiry()
{
return ResetSession();
}
public static int ResetSession()
{
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.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;
return timeout;
}
VB
<WebMethod> _
Public Shared Function SetSessionExpiry() As Integer
Return ResetSession()
End Function
Public Shared Function ResetSession() As Integer
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache)
HttpContext.Current.Session("Reset") = True
Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration("~/Web.Config")
Dim section As SessionStateSection = DirectCast(config.GetSection("system.web/sessionState"), SessionStateSection)
Dim timeout As Integer = CInt(section.Timeout.TotalMinutes) * 1000 * 60
Return timeout
End Function
Screenshot