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

用户名

我有以下会话超时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>
透明金

为什么不调用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#代码隐藏:您需要参考以下内容:

using System.Web.Services;

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在使用jQuery将数据发送到数据库时,如何使按钮处于活动状态?

如何使用Jquery和Ajax将Json数据发送到数据库中?

如何在jQuery中以透明的方式将数据发送到数据库中的用户?

使用Jquery / ajax将数据发送到数据库

使用 Laravel 身份验证和 Ajax 将数据发送到数据库表时获取未经身份验证的消息

如何通过Asp.net中的SqlDataSource存储过程参数将Int32输入发送到数据库

如何在ASP.NET MVC中使用邮政将电子邮件发送到数据库中的多个电子邮件地址

通过参数化的插入语句将错误的数据从VB.net发送到Mysql数据库

如何使用 React 将数据从 <option> 发送到另一个页面和搜索数据库

我正在尝试使用jquery和ajax将多个数据发送到另一个页面,以检查数据库中是否存在值

无法使用 Jquery 将数据从视图发送到控制器 Asp.net MVC

如何将数据从JQuery Ajax发送到ASP.NET页?

尝试使用HTML和Node.js将登录表单输入发送到数据库

我正在尝试使用 laravel 将多个标题和多个文件发送到数据库中,但出现错误

$ ajax和jQuery将数据发送到php

用户输入,发送到数据库之前先进行清理和消毒

如何在不刷新页面的情况下将文本和图像文件数据同时发送到 MySQL 数据库

如何将数据从使用 javascript 和 ASP.NET 生成的字段发送到 Microsoft SQL?

使用React JS和Laravel将带有图像文件的POST请求发送到数据库时出现内部服务器错误

使用JQuery Mobile使用PHP,POST和JSON将数据发送到服务器时出现问题

如何通过ajax-POST使用jquery将多个字符串发送到数据库到php文件?

ASP.NET Core 将数据从表发送到模态

将数据从wp froms发送到asp.net系统

ASP.NET Core 2.1 MVC使用XMLHttpRequest将数据从JavaScript发送到Action方法

如何使用ASP.NET Core 3.0将数据从Controller发送到cshtml

jQuery ajax调用,以在单击a href时将数据发送到该方法

单击按钮以使用 jQuery 将数据发送到服务器时网页消失

jquery-步骤| 在ajax内容加载时将数据发送到服务器

无法将多个文件从JQuery AJAX发送到ASP.NET MVC