检测用户何时在 Page_Load 事件中离开页面

发布于 2024-12-07 20:28:51 字数 112 浏览 0 评论 0原文

当用户离开页面时,它会再次触发 Page_Load 事件。我如何在 code_behind 中知道这种情况正在发生,以便可以避免运行自定义函数,因为 Page_Load 中的代码在离开页面时实际上并不需要运行?

When a user leaves a page it fires of the Page_Load event again. How can I tell in code_behind that this is happening so in can avoid running the custom functions as the code in the Page_Load does not really need to be ran when leaving the page?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事和酒 2024-12-14 20:28:51

当用户离开页面时,它会再次触发 Page_Load 事件

离开页面是什么意思?关闭浏览器或单击浏览器上的“后退”按钮?或者转移到另一个页面? Page_Load 方法在加载页面或同一页面上发生回发时触发,但不会离开该页面。

在开始任何操作之前,您可以(并且应该)确保客户端仍处于连接状态并使用 HttpResponse.IsClientConnected 属性。

当满足以下条件时,IsClientConnected 属性返回 false:

  1. 与客户端的连接已终止。如果调用 Close 方法,或者客户端停止执行网页或浏览到另一个页面,则可能会发生这种情况。
  2. 处理请求的 HttpWorkerRequest 对象为 null 或 HttpWorkerRequest.IsClientConnected 方法返回 false。如果自定义 HttpWorkerRequest 对象处理该请求,则可以根据自定义条件设置 HttpWorkerRequest.IsClientConnected 方法。例如,自定义工作线程请求可能在一段时间后强制超时。

http://msdn.microsoft.com/en-us /library/system.web.httpresponse.isclientconnected.aspx

编辑

切换选项卡通常会触发正常的回发,要检测它,您应该使用:

private void Page_Load(object sender, System.EventArgs e)
{
    if (!Page.IsPostback)
    {
       //this is first load of this page
    }
}

When a user leaves a page it fires of the Page_Load event again

What do you mean by leaving a Page? Closing the browser or clicking Back button on browser? Or moving to another Page? Page_Load method is fired when on Loading Page or when Postback occurs on the same Page, but not leaving it.

Before you start any operations, you can (and you should) ensure that client is still connected and use HttpResponse.IsClientConnected property.

The IsClientConnected property returns false when the following conditions are true:

  1. The connection to the client was terminated. This can occur if the Close method was invoked, or if the client stopped execution of the Web page or browsed to another page.
  2. The HttpWorkerRequest object that is handling the request is null or the HttpWorkerRequest.IsClientConnected method returns false. If a custom HttpWorkerRequest object handles the request, then the HttpWorkerRequest.IsClientConnected method might be set based on custom criteria. For example, the custom worker request might force a time-out after a period of time.

http://msdn.microsoft.com/en-us/library/system.web.httpresponse.isclientconnected.aspx

EDIT

Switching tabs usually fires normal Postback, to detect it you should use:

private void Page_Load(object sender, System.EventArgs e)
{
    if (!Page.IsPostback)
    {
       //this is first load of this page
    }
}
執念 2024-12-14 20:28:51

如果我正确理解这个问题,您正在寻找 IsPostBack 属性:

 private void Page_Load()
 {
    if (!IsPostBack)
    {
        // Any code here will only run the first time the page is loaded  
    }
}

If I understand the question correctly, you're looking for the IsPostBack property:

 private void Page_Load()
 {
    if (!IsPostBack)
    {
        // Any code here will only run the first time the page is loaded  
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文