2017-08-24 57 views
-1

我希望會話超時在單擊模式彈出窗口的「是」按鈕時重置。目前JavaScript代碼只是隱藏Modal彈出窗口。其他一切都在按照應有的方式工作。由於單擊模式彈出窗口的「是」按鈕時重置會話超時

HTLML代碼

<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"> 
    </asp:ToolkitScriptManager> 
    <h3>Session Idle:&nbsp;<span id="secondsIdle"></span>&nbsp;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&nbsp;<span id="seconds"></span>&nbsp;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> 

我想的JavaScript代碼來處理目前對話重置代碼只是隱藏模式彈出

JavaScript代碼

<script type="text/javascript"> 
    function SessionExpireAlert(timeout) { 
     var seconds = timeout/1000; 
     document.getElementsByName("secondsIdle").innerHTML = seconds; 
     document.getElementsByName("seconds").innerHTML = seconds; 
     setInterval(function() { 
      seconds--; 
      document.getElementById("seconds").innerHTML = seconds; 
      document.getElementById("secondsIdle").innerHTML = seconds; 
     }, 1000); 
     setTimeout(function() { 
      //Show Popup before 20 seconds of timeout. 
      $find("mpeTimeout").show(); 
     }, timeout - 20 * 1000); 
     setTimeout(function() { 
      window.location = "Login.aspx"; 
     }, timeout); 
    }; 
    function ResetSession() { 
     // Hide Modal Popup 
     $find('mpeTimeout').hide(); 

     // Reset Session Here 

    } 

    </script> 

C#代碼

protected void Page_Load(object sender, EventArgs e) 
{ 
     Response.Cache.SetCacheability(HttpCacheability.NoCache); 
    if (this.IsPostBack) 
    { 
     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; 
     ClientScript.RegisterStartupScript(this.GetType(), "SessionAlert", "SessionExpireAlert(" + timeout + ");", true); 
    } 
    } 
+0

使用jQuery/AJAX。爲什麼回傳? –

+0

@ T.S任何代碼建議請 –

+0

我給你的想法 - 谷歌是充滿了這些東西(代碼)。在aspx頁面中創建web方法,並使用Ajax包裝器之一調用它(jQuery是一個有據可查的開源js庫)。 –

回答

0
 <script src="/jQuery/Plugins/js.cookie-2.1.3.min.js"></script> 
       <script> 
        var popupTimeOut; 
        var fullSessionTimeOut; 
        var timer; 
        var CookiesTime; 
        var localMillisUTC; 
        var counter = 120; 
        var PopUpShown = false; 

        function getCookiesTime() { 
         CookiesTime = Cookies.get('SQSessionTimeOutTime'); 
         if (typeof (CookiesTime) == 'undefined') { 
          CookiesTime = 0; 
          clearInterval(timer); 
          clearInterval(popupTimeOut); 
         } 
         return CookiesTime; 
        } 

        function timedCount(timeout) { 

         popupTimeOut = setInterval(function() { 
          localMillisUTC = (new Date()).getTime();//this is UTC since 1970 
          var difference = getCookiesTime() - localMillisUTC; 

          if (difference < 120000) { 
           if (!PopUpShown) { 
            showPopTimerPopUp(); 
           } 
          } else { 
           clearInterval(timer); 
           $find('mpeTimeout').hide(); 
           counter = 120; 
          } 
          if (difference <= 0) { 
           SessionTimeOut(); 
           clearInterval(timer); 
           $find('mpeTimeout').dispose(); 
           clearInterval(popupTimeOut); 
          } 
         }, 10000); 
        } 
        function startSessionCount(timeout) { 
         if (navigator.cookieEnabled) { 
          timedCount(timeout); 
         } 
        } 

        function showPopTimerPopUp() 
        { 
         PopUpShown = true; 
         $find('mpeTimeout').show();        
         timer = setInterval(function() { 
          localMillisUTC = (new Date()).getTime();//this is UTC since 1970 
          var countDownTimer = (getCookiesTime() - localMillisUTC); 
          var counter = parseInt(((getCookiesTime() - localMillisUTC)/1000)); 
          if (countDownTimer <= 0) { 
           SessionTimeOut(); 
           clearInterval(timer); 
           $("#seconds").html(counter); 
          } 
          else if (countDownTimer > 120000) 
          { 
           PopUpShown = false; 
           $find('mpeTimeout').hide(); 
          } 
          else 
          { 
           $("#seconds").html(counter); 
          } 
         }, 1000); 
        } 
        function resetSession() { 
         clearInterval(timer); 
         keepServerSideSessionAliveAjax(); 
         PopUpShown = false; 
        } 
        function SessionTimeOut() { 
         var urlPath = $(location).attr('pathname'); 
         var erroMessage= '<%=GetTranslation("msg=Your session timed out, please login below", "lsessionTimeoutmsg")%>'; 
         window.location = encodeURI("../SessionEnd.aspx?RedirectUrl=" + urlPath + "&" + erroMessage); 
        } 
        function noSessionExp() { 
         clearInterval(popupTimeOut); 
        } 
        function keepServerSideSessionAliveAjax() { 
         $.ajax({ 
          type: "POST", 
          url: "/Main/SessionAliveKeeper.aspx/GetSessionTimer", 
          data: "{}", 
          contentType: "application/json; charset=utf-8", 
          dataType: "json", 
          success: function (result) { 
          } 
         }); 
        } 
     </script> 


<ajaxToolkit:ModalPopupExtender ID="mpeTimeout" runat="server" BehaviorID="mpeTimeout" TargetControlID="lnkFake" PopupControlID="pnlPopup" BackgroundCssClass="modalBackground" OkControlID="btnYes" CancelControlID="btnNo" OnOkScript="resetSession();" OnCancelScript="noSessionExp()" /> 
        <asp:LinkButton ID="lnkFake" runat="server" /> 
        <asp:Panel ID="pnlPopup" runat="server" CssClass="modalPopup" Style="display: none" SkinID="modal"> 
         <div class="frame"> 
          <div class="Popupcontainer" style="width: 220px"> 
           <div id="popupDaysHeader" class="header" style="cursor: move"> 
            <div class="msg"> 
             <%= GetTranslation("Session Expiring!","lheaderSessionExp")%> 
            </div> 
            <asp:LinkButton ID="LinkButton4" runat="server" CssClass="close" OnClientClick="$find('mpeTimeout').hide(); return false;" /> 
           </div> 
           <div class="Popupbody"> 
            &nbsp;&nbsp; 
            <%= GetTranslation("Your Session will expire in","ltxtSessionExp1") %>&nbsp;<span id="seconds"></span>&nbsp;seconds.<br /> 
            &nbsp;&nbsp; 
            <%=GetTranslation("Do you want to reset?","ltxtTransaltionExp3") %><br /> 
            <br /> 
            <span style="align-items:right;"> 
             &nbsp;&nbsp; 
             <asp:Button ID="btnYes" runat="server" Text="Yes" CssClass="yes" /> 
             <asp:Button ID="btnNo" runat="server" Text="No" CssClass="no" OnClientClick="SessionTimeOut()"/> 
            </span> 
           </div> 
           <div style="text-align: right"> 
           </div> 
          </div> 
         </div> 
        </asp:Panel> 

這是整個ASCX頁,每當用戶點擊是服務器超時將重置您只需要更新的cookie。這是修改你的代碼

<%@ Register Src="~/UserControls/SessionTimeoutPopup/SessionTimeoutPopup.ascx" TagName="SessionTimeoutPopup" TagPrefix="st" %> 

您可以將此行添加到任何網頁,它會工作..there一定缺少的東西,但我想你看着辦吧,如果你需要更多的幫助,讓我知道..

+0

感謝您的回覆。看起來像GetTranslation丟失。我收到錯誤「名稱'GetTranslation'在當前上下文中不存在'任何幫助如何解決它。 –

+0

只是刪除該功能,我們用它來翻譯。 @EricMbiada,如果你認爲ans有幫助,請投票支持 – nikunjM

0

嘿,你可以在你的母版頁或其他使用這些腳本,

<script type="text/javascript"> 

    var sess_pollInterval = 60000; 
    var sess_expirationMinutes = 20; 
    var sess_warningMinutes = 15; 
    var sess_intervalID; 
    var sess_lastActivity; 

    function initSession() { 
     sess_lastActivity = new Date(); 
     sessSetInterval(); 
     $(document).bind('keypress.session', function (ed, e) { 
      sessKeyPressed(ed, e); 
     }); 
    } 

    function sessSetInterval() { 
     sess_intervalID = setInterval('sessInterval()', sess_pollInterval); 
    } 

    function sessClearInterval() { 
     clearInterval(sess_intervalID); 
    } 

    function sessKeyPressed(ed, e) { 
     sess_lastActivity = new Date(); 
    } 

    function sessLogOut() { 
     window.location.href = 'Default.aspx'; 
    } 

    function sessInterval() { 
     var now = new Date(); 
     //get milliseconds of differneces 
     var diff = now - sess_lastActivity; 
     //get minutes between differences 
     var diffMins = (diff/1000/60); 

     if (diffMins >= sess_warningMinutes) { 
      //wran before expiring 
      //stop the timer 
      sessClearInterval(); 
      //promt for attention 
      var active = confirm('Your session will expire in ' + (sess_expirationMinutes - sess_warningMinutes) + 
       ' minutes (as of ' + now.toTimeString() + '), press OK to remain logged in ' + 
       'or press Cancel to log off.'); 
      if (active == true) { 
       now = new Date(); 
       diff = now - sess_lastActivity; 
       diffMins = (diff/1000/60); 

       if (diffMins > sess_expirationMinutes) { 
        sessLogOut(); 
       } 
       else { 
        initSession(); 
        sessSetInterval(); 
        sess_lastActivity = new Date(); 
       } 
      } 
      else { 
       sessLogOut(); 
      } 
     } 
    } 
    </script> 
相關問題