Is there any way to know that user leaving a page with asp.net?

后端 未结 5 1929
遥遥无期
遥遥无期 2020-12-10 08:54

My question is a little bit tricky. at least I think. Maybe not, anyway. I want to know if there\'s any way to know if the user is leaving the page. Whatever if he clicks \"

相关标签:
5条回答
  • 2020-12-10 09:21

    Euuwww ... well ok. My problem it's because I've stored in a Session variable an objects (with a bunch of search criterias). By storing it in a session variable, if the user go back to my search page result, it will list back the same result as last time. But, after some reflexion (not in code but in my head and after seen your answer :oP), I will add a boolean property in my object which will mean something like "IsDirty" or "HasBeenListed". And when i will load back, if its set to dirty, it's gonna redirect back to another page.

    Thank David.

    0 讨论(0)
  • 2020-12-10 09:22

    There really is no way to do it. No event is fired when the browser goes back.

    You can do it with Javascript, but it is difficult at best.

    See the question here.

    This script will also work. It was found here

    <script>
    
    window.onbeforeunload = function (evt) {
    var message = ‘Are you sure you want to leave?’;
    if (typeof evt == ‘undefined’) {
    evt = window.event;
    }
    if (evt) {
    evt.returnValue = message;
    }
    return message;
    }
    
    </script> 
    
    0 讨论(0)
  • 2020-12-10 09:37

    As mentioned, you can do it unreliably in JavaScript, but I am assuming from the wording of your question that you want to do it in the Server-side code ("the code-behind").

    That is impossible since the code-behind is running on a different computer (the web server) and has no idea what is going on at the user's computer unless the browser sends a request back to the server (which requires some Javascript or other client side code).

    If you must, use the session timeout events to clean up any state information you are storing beyond the lifetime of a request.

    0 讨论(0)
  • 2020-12-10 09:39

    You can use javascript to tell the server when the user leaves the page. But the webserver washes it's hands of the page once it leaves the server, while the user might keep the page open for a week.

    If you use javascript on the page to fire off a notice to your server when the page unloads you can take some action. But, you can't tell if he's leaving your page for another one of your pages, another website, or closing the browser.

    And that last notice isn't guaranteed to always be sent, so you can't rely on it completely.

    So using the javascript notice to clean up objects (caches or sessions) is a flawed system. You're better with cache & session invalidation strategies that are independent of the onunload notice.

    0 讨论(0)
  • 2020-12-10 09:43

    If the goal of this is to cleanup objects in your code-behind, is it good enough to rely on the session timeout? It would be a 20 minute delay before cleaning up those objects (or whatever you have your session timeout set for).

    But it's an easy and dependable way to do it. In your Global.asax file you'd just do this:

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
      ' Clean up stuff
    End Sub
    
    0 讨论(0)
提交回复
热议问题