如何在ASP.Net中处理页面关闭?

时间:2021-09-08 05:15:56

I want to do something when user closes the page from "browser close". Is there a way to handle that event in server side?

当用户从“浏览器关闭”关闭页面时,我想做某事。有没有办法在服务器端处理该事件?

3 个解决方案

#1


You could add a window unload handler that does some AJAX to the server. I don't know how reliable it would be in practice, though.

您可以添加一个窗口卸载处理程序,它可以为服务器执行某些AJAX。不过,我不知道它在实践中有多可靠。

#2


You can try handling the javascript OnUnload Event and call the the __dopostback() function for client script postback.

您可以尝试处理javascript OnUnload事件并调用__dopostback()函数进行客户端脚本回发。

To clarify for the comments, it's not a foolproof pattern by any means, but maybe helpful depending on what the programmer is trying to do exactly. Even if the postbacks are checked and filtered on the server-side, it would need to be an action that does not have to run only once and always.

为了澄清这些评论,它无论如何都不是一个万无一失的模式,但可能会有所帮助,这取决于程序员正在努力做什么。即使在服务器端检查和过滤回发,它也需要是一个不必一次只运行一次的动作。

#3


You also have to be careful what you use this event for. Do you need it to fire if the user kills the browser through Task Manager? Do you need it to fire when he logs off?

您还必须小心使用此事件。如果用户通过任务管理器杀死浏览器,您是否需要触发?注销后你需要点火吗?

I think that, in most browsers, it will not fire in either of those two cases.

我认为,在大多数浏览器中,它不会在这两种情况下触发。

#1


You could add a window unload handler that does some AJAX to the server. I don't know how reliable it would be in practice, though.

您可以添加一个窗口卸载处理程序,它可以为服务器执行某些AJAX。不过,我不知道它在实践中有多可靠。

#2


You can try handling the javascript OnUnload Event and call the the __dopostback() function for client script postback.

您可以尝试处理javascript OnUnload事件并调用__dopostback()函数进行客户端脚本回发。

To clarify for the comments, it's not a foolproof pattern by any means, but maybe helpful depending on what the programmer is trying to do exactly. Even if the postbacks are checked and filtered on the server-side, it would need to be an action that does not have to run only once and always.

为了澄清这些评论,它无论如何都不是一个万无一失的模式,但可能会有所帮助,这取决于程序员正在努力做什么。即使在服务器端检查和过滤回发,它也需要是一个不必一次只运行一次的动作。

#3


You also have to be careful what you use this event for. Do you need it to fire if the user kills the browser through Task Manager? Do you need it to fire when he logs off?

您还必须小心使用此事件。如果用户通过任务管理器杀死浏览器,您是否需要触发?注销后你需要点火吗?

I think that, in most browsers, it will not fire in either of those two cases.

我认为,在大多数浏览器中,它不会在这两种情况下触发。