当sessiom超时到期时,将状态发送到数据库。 ASP.Net和Jquery

时间:2014-03-28 15:42:22

标签: jquery mysql asp.net

我有以下会话超时jquery语句:

 <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>
     <script type="text/javascript">
         idleTime = 0;

         $(document).ready(function () {

             // Inkrementerer every minute
             var idleInterval = setInterval(timerIncrement, 1000); // 1 minute


             // reset the counter when mouse moved or key pressed  
             $(this).mousemove(function (e) {
                 idleTime = 0;
             });
             $(this).keypress(function (e) {
                 idleTime = 0;
             });
         });

         function timerIncrement() {

             document.getElementById("<%= lblSessionTime.ClientID %>").innerText =         idleTime;
             idleTime = idleTime + 1;
             if (idleTime > 19) { // 20 minutes

                 // Send status to Database

             }

         }

</script>

当空闲时间超过20分钟我想向MySQL数据库发送状态时,该页面现在处于离线状态。我无法弄明白如何使用JQuery。有什么想法吗?

更新2:

所有aspx代码:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Drivhus 1.aspx.cs" Inherits="FS07.Drivhus_1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
   <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>
         <script type="text/javascript">
             idleTime = 0;

             $(document).ready(function () {

                 // Inkrementerer inaktiv tilstand counteren hvert minut.
                 var idleInterval = setInterval(timerIncrement, 1000); // 1 minute


                 // Nulstiller counteren når musen er bevæget eller der bliver tastet.  
                 $(this).mousemove(function (e) {
                     idleTime = 0;
                 });
                 $(this).keypress(function (e) {
                     idleTime = 0;
                 });
             });

             function timerIncrement() {

                 document.getElementById("<%= lblSessionTime.ClientID %>").innerText = idleTime;
                 idleTime = idleTime + 1;
                 if (idleTime > 19) { // 20 minutes

                     // Send status to Database
                     PageMethods.SendStatustoDB();
                     alert("Connected");
                 }

             }

    </script>
</head>
<body>
    <form id="form1" runat="server"> 
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>     
    <div>
     <h6>Session time left:&nbsp;<asp:Label ID="lblSessionTime"  runat="server"></asp:Label>&nbsp;minutes.</h6>
     <h2>Her vises SCADA GUI</h2>

    </div>
    </form>
</body>
</html>

2 个答案:

答案 0 :(得分:2)

为什么不拨打C#WebMethod?这将是一个AJAX方法,需要使用ScriptManager ..

ASPX:确保在 FORM 标记内添加ScriptManager对象并设置EnablePageMethods = true

<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>

    /* REST OF YOUR HTML */

</form>

JavaScript:您需要修改 timerIncrement 函数并调用 C#WebMethod

function timerIncrement() {
    document.getElementById("<%= lblSessionTime.ClientID %>").innerText = idleTime;
    idleTime = idleTime + 1;

    if (idleTime > 19) { // 20 minutes

        // Call C# WebMethod to send status to Database
        PageMethods.SendStatustoDB();
    }
}

C#Code-Behind:您需要参考以下内容:

using System.Web.Services;

然后添加以下方法并确保在方法声明之前放置[WebMethod]

[WebMethod]
public static void SendStatustoDB()
{
    // code to connect to DB and set status!
}

答案 1 :(得分:0)

这似乎很容易。此代码将在与ajax调用中提供的URL相同的域中运行:

在您的客户中:

<script>
  function notifySessionExpired(somedata){
      var url = "some-script-in-your-server.php";
      $.ajax({
          url: url, type: 'post',  data: { args: somedata , info: 'notifyexpire' } ,
          success: function(result){
             $('#log').html("result was: "+result);
          },
          error: function(e){
              $('#log').html(e.responseText);
          }
      });
  }
</script>

在您的服务器中(some-script-in-your-sever.php)

--begin-of-file--
if(isset($_POST["info"])){
   if("notifysession" == $_POST["info"]){
      $args = $_POST["args"];
      echo fireYourServerSideEventAndDoSomething($args);
   }
}
--end-of-file--