在asp.net的页面关闭上运行一个sql命令 [英] run an sql command on page close in asp.net

查看:63
本文介绍了在asp.net的页面关闭上运行一个sql命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想记录用户进入页面的时间以及他离开此页面的时间。



输入时间由Sql记录在page_load事件中命令。



我有一个退出按钮。如果用户单击该按钮并退出,则在button_click事件上运行Sql命令。



如果用户离开页面,他会收到一条警告消息以便返回并单击记录退出时间的按钮。



当用户在页面关闭时忽略此警报消息并且存在时问题就出现了。



当用户通过单击X离开页面并离开页面时,有什么方法可以运行Sql命令。



我正在使用ASP.NET和C#



感谢您的帮助。

I would like to record the time when a user enters a page and also the time when he leaves this page.

The enter time is recorded by an Sql Command in page_load event.

I have a button for exit. If a user clicks that button and exits, the Sql command is run on button_click Event.

If a user leaves the page he gets an alert message to get back and click the button to record his exit time.

The problem comes when the user ignores even this alert message on page close and exists.

Is there any way i can run an Sql Command when a user leaves the page by clicking on 'X' and leaves the page.

I am using ASP.NET with C#

Thanks for your help.

推荐答案

可靠吗?没有。

如果他关掉电源会怎么样?

断开他的宽带线?

Force关闭他的浏览器?

等待20分钟直到会话结束?

按下返回按钮?



如果这对您来说非常重要,然后你需要在每分钟(例如)在客户端上运行的Javascript中添加一个计时器,并向服务器报告页面仍处于打开状态 - 然后记录时间。如果你停止获取它,那么页面可以被假定为关闭,最后记录的时间给出了持续时间。
Reliably? No.
What if he turns off the power?
Disconnects his broadband line?
Force closes his browser?
Waits 20 minutes until the session has expired?
Presses the "Back" button?

If this is seriously important to you, then you need to look at adding a timer in Javascript that runs on the client every minute (say) and reports to the server that the page is still open - and record the time then. If you stop getting it, then the page can be assumed to be "closed" and the last recorded time gives the duration.


之前我做了同样的事情。



基本上,运行web api为我记录这些数据并不是关键任务,所以我选择了这个解决方案:



1.我为特定页面创建一个cookie,并记录用户访问该页面时的开始时间。 (在我的情况下,每个页面都有一个唯一的cookie)。

2.在任何页面重定向,我访问所有其他未清理的cookie并记录对我的Web API的调用以注册所有时间饼干,也清理它们。这样我就可以确保我保证数据的安全。

3. OnBeforeUnload事件有一个Web API调用和cookie的清理,这样当用户关闭页面和/或点击链接时,我将尝试调用Web API来记录消息。

4.如果出现链接问题或电源问题,我的cookie将保留在上次从Timer更新的浏览器上,并且在用户下次访问同一网站时,我将更新数据到服务器。



请记住,依靠数据并不是一个好主意。例如,如果你真的需要确保在离开页面时调用你的web api,这不是一个好主意。但它对我有用。
I did the same thing before.

Basically, it is not mission critical to run a web api to log this data for me, so I opted for this solution :

1. I create a cookie for a particular page and log the start time when an user visits the page. (In my case every page has a unique cookie).
2. On any page redirect, I am accessing all other cookie which is not cleaned and log a call to my Web API to register time of all the cookie and also clean them. This way I can ensure that I keep the data safe.
3. OnBeforeUnload event there is a Web API call and cleanup of the cookie such that when an user closes the page and / or clicks on a link, I will try to call a Web API to log the message.
4. If there is a link issue or power issue, my cookie will remain on the browser updated with last time from Timer and the very next time the user visits to the same website, I will update the data to the server.

Remember, it is not a good idea to rely on the data. For instance, if you really require to ensure that your web api is called while leaving the page, this wont be a good idea tough. But it works for me.


这篇关于在asp.net的页面关闭上运行一个sql命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆