gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 06:39:02 25 4
gpt4 key购买 nike

我有以下 session 超时 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!
}

关于jquery - 当 session 超时到期时,将状态发送到数据库。 ASP.Net 和 Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22717339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com