Link to home
Start Free TrialLog in
Avatar of Saroj13
Saroj13

asked on

Display SessionTimeout warning dialog appears 2 minutes before Session timeout. How to display dialog on parent page and reset again on any activities inside frame in asp.net, c#

Hi,

Problem : Timer not working on the dialog properly on the parent page default.aspx. Countdown timer changing time 129 98 78 90 120 67 89 etc like that...

problem2. clicking on any button in details frame will reset session again. Dialog will again disappear 2minutes before timeout and it should show 120 119 118 seconds left....

How to achieve that. Please change my code...

Code

MasterPage.aspx

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Masterpage.master.cs" Inherits="Masterpage" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp"%>
<%@ Register Src="~/SessionTimeout.ascx" TagPrefix="uc1" TagName="SessionTimeout" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <asp:ContentPlaceHolder id="head" runat="server">
   </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
  
       <div id="mainContent" >
        <asp:ContentPlaceHolder id="MainContent" runat="server"   >      
        </asp:ContentPlaceHolder>
    </div>
     <uc1:SessionTimeout id="SessionTimeout" runat="server"></uc1:SessionTimeout>
     
    </form>
</body>
</html>
 
SessionTimeout.ascx
 
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SessionTimeout.ascx.cs" Inherits="SessionTimeout" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp"%>
<script src="scripts/jquery-1.12.4.js" type="text/javascript"></script>
<script type="text/javascript" src="scripts/Functions.js"></script>
 
 
      <asp:ScriptManager ID="sm" runat="server" EnablePageMethods="true" />
                  <asp:LinkButton ID="lnkSession" runat="server" />
<asp:ModalPopupExtender ID="mpeTimeout" BehaviorID ="mpeSessionTimeout" runat="server" PopupControlID="pnlPopup" TargetControlID="lnkSession"
   OkControlID="btnOK"  BackgroundCssClass="modalBackground" >
</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&nbsp;<span id="seconds"></span>&nbsp;seconds.<br />
     </div>
 
    <div class="footer" align="right">
    
        <asp:Button ID="btnOK" runat="server" Text="OK" CssClass="Savebutton" CausesValidation="false" />
             
    </div>
</asp:Panel>
 

SessionTimeout.ascx.cs

using System.Configuration;
using System.Web.Configuration;
using System.Web.Services;
 
    public partial class SessionTimeout : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Session Timeout Warning Dialog
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            if (!this.IsPostBack)
 
            {
                Session["Reset"] = true;
                int timeout = GetSessionTimeout();
                Page.ClientScript.RegisterStartupScript(this.GetType(), "SessionAlert", "AdminSessionTimeoutAlert(" + 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="3"/>

Javascript  Functions.js

function AdminSessionTimeoutAlert(timeout) {
   var seconds = timeout / 1000;
 
     window.parent.document.getElementsByName("seconds").innerHTML = seconds;
 
    setInterval(function () {
 
        seconds--;
 
        window.parent.document.getElementById("seconds").innerHTML = seconds;
 
      }, 1000);
 
    setTimeout(function () {
       window.parent.$find("mpeSessionTimeout").show();
 
    }, timeout - 120 * 1000);
 
 
    setTimeout(function () {
 
        window.top.location.href = "SessionExpired.aspx";
 
    }, timeout);
 
};
 
function ResetSession() {
    PageMethods.ResetSession(OnSuccess);
    return false;
}
function OnSuccess(response, userContext, methodName) {
    AdminSessionTimeoutAlert(response);
}
 
 

Parent Page default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="default" MasterPageFile="~/Masterpage.Master"%>
 
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <div class="container">
      <iframe id="frameName1" name="frameName1" src="index.aspx" frameborder="0"></iframe>
        </div>
 
<div class="container1">
     <iframe id="frameName2" src=" details.aspx" frameborder="0"></iframe>
</div> 
</asp:Content>
 
Details.aspx
 
<%@ Page language="c#" Inherits="Details" Codebehind="DEtails.aspx.cs" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
  <HEAD>
      <script type="text/javascript" src="scripts/Functions.js"></script>
      <script src="scripts/jquery-1.12.4.js" type="text/javascript"></script>
 
 
  </HEAD>
  <body>
      
    <form id="Form1" method="post" runat="server">
              <asp:ScriptManager ID="sm" runat="server" EnablePageMethods="true" />
              <INPUT type="text" name="txtCategoryName"  size="20" maxlength="50" ID="txtCategoryName" runat="server">
 
<asp:Button text="Submit" ID="btnSubmit1" runat="server"  OnClientClick="ResetSession();"/>
     </form>
      
  </body>
</HTML>
 
Details.aspx
 
using System.Web.Services;
using System.Configuration;
using System.Web.Configuration;
 
       public partial class Details: System.Web.UI.Page
       {
              protected void Page_Load(object sender, System.EventArgs e)
              {
 
            if (!IsPostBack)
            {
                Session["Reset"] = true;
                int timeout = GetSessionTimeout();
                Page.ClientScript.RegisterStartupScript(this.GetType(), "SessionAlert", "AdminSessionTimeoutAlert(" + 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);
        }
 

Open in new window


Hi,

Problem : Timer not working on the dialog properly on the parent page default.aspx. Countdown timer changing time 129 98 78 90 120 67 89 etc like that...

problem2. clicking on any button in details frame will reset session again. Dialog will again disappear 2minutes before timeout and it should show 120 119 118 seconds left....

How to achieve that. Please change my code...

Code

MasterPage.aspx

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Masterpage.master.cs" Inherits="Masterpage" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp"%>
<%@ Register Src="~/SessionTimeout.ascx" TagPrefix="uc1" TagName="SessionTimeout" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <asp:ContentPlaceHolder id="head" runat="server">
   </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
  
       <div id="mainContent" >
        <asp:ContentPlaceHolder id="MainContent" runat="server"   >      
        </asp:ContentPlaceHolder>
    </div>
     <uc1:SessionTimeout id="SessionTimeout" runat="server"></uc1:SessionTimeout>
     
    </form>
</body>
</html>
 
SessionTimeout.ascx
 
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SessionTimeout.ascx.cs" Inherits="SessionTimeout" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp"%>
<script src="scripts/jquery-1.12.4.js" type="text/javascript"></script>
<script type="text/javascript" src="scripts/Functions.js"></script>
 
 
      <asp:ScriptManager ID="sm" runat="server" EnablePageMethods="true" />
                  <asp:LinkButton ID="lnkSession" runat="server" />
<asp:ModalPopupExtender ID="mpeTimeout" BehaviorID ="mpeSessionTimeout" runat="server" PopupControlID="pnlPopup" TargetControlID="lnkSession"
   OkControlID="btnOK"  BackgroundCssClass="modalBackground" >
</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&nbsp;<span id="seconds"></span>&nbsp;seconds.<br />
     </div>
 
    <div class="footer" align="right">
    
        <asp:Button ID="btnOK" runat="server" Text="OK" CssClass="Savebutton" CausesValidation="false" />
             
    </div>
</asp:Panel>
 

SessionTimeout.ascx.cs

using System.Configuration;
using System.Web.Configuration;
using System.Web.Services;
 
    public partial class SessionTimeout : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Session Timeout Warning Dialog
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            if (!this.IsPostBack)
 
            {
                Session["Reset"] = true;
                int timeout = GetSessionTimeout();
                Page.ClientScript.RegisterStartupScript(this.GetType(), "SessionAlert", "AdminSessionTimeoutAlert(" + 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="3"/>

Javascript  Functions.js

function AdminSessionTimeoutAlert(timeout) {
   var seconds = timeout / 1000;
 
     window.parent.document.getElementsByName("seconds").innerHTML = seconds;
 
    setInterval(function () {
 
        seconds--;
 
        window.parent.document.getElementById("seconds").innerHTML = seconds;
 
      }, 1000);
 
    setTimeout(function () {
       window.parent.$find("mpeSessionTimeout").show();
 
    }, timeout - 120 * 1000);
 
 
    setTimeout(function () {
 
        window.top.location.href = "SessionExpired.aspx";
 
    }, timeout);
 
};
 
function ResetSession() {
    PageMethods.ResetSession(OnSuccess);
    return false;
}
function OnSuccess(response, userContext, methodName) {
    AdminSessionTimeoutAlert(response);
}
 
 

Parent Page default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="default" MasterPageFile="~/Masterpage.Master"%>
 
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <div class="container">
      <iframe id="frameName1" name="frameName1" src="index.aspx" frameborder="0"></iframe>
        </div>
 
<div class="container1">
     <iframe id="frameName2" src=" details.aspx" frameborder="0"></iframe>
</div> 
</asp:Content>
 
Details.aspx
 
<%@ Page language="c#" Inherits="Details" Codebehind="DEtails.aspx.cs" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
  <HEAD>
      <script type="text/javascript" src="scripts/Functions.js"></script>
      <script src="scripts/jquery-1.12.4.js" type="text/javascript"></script>
 
 
  </HEAD>
  <body>
      
    <form id="Form1" method="post" runat="server">
              <asp:ScriptManager ID="sm" runat="server" EnablePageMethods="true" />
              <INPUT type="text" name="txtCategoryName"  size="20" maxlength="50" ID="txtCategoryName" runat="server">
 
<asp:Button text="Submit" ID="btnSubmit1" runat="server"  OnClientClick="ResetSession();"/>
     </form>
      
  </body>
</HTML>
 
Details.aspx
 
using System.Web.Services;
using System.Configuration;
using System.Web.Configuration;
 
       public partial class Details: System.Web.UI.Page
       {
              protected void Page_Load(object sender, System.EventArgs e)
              {
 
            if (!IsPostBack)
            {
                Session["Reset"] = true;
                int timeout = GetSessionTimeout();
                Page.ClientScript.RegisterStartupScript(this.GetType(), "SessionAlert", "AdminSessionTimeoutAlert(" + 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);
        }
 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of David Favor
David Favor
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Marking my answer.

Poster seems to have stopped posting.