Search code examples
c#asp.net.net-2.0

Determine when a user "leaves" a page/site in ASP.NET?


how I can call database stored procedure if someone is leaving page? For example, I have some kind of cart, when people add items to cart it must be locked until save, but if you turn page of, items are reserved in database, how to avoid it? How to make items unreserved if person turn off page without save? Thank you


Solution

  • Disclaimer: the following suggests a solution for the problem, not the question.


    The only way I know of to reliably handle something like this -- ensuring that long cross-transaction "locks" (or "tickets") are released -- is to use a service to "release" (or "clear") items periodically.

    That is, instead of responding to a "logout" event, simply, after say an (initial) 1 hour reservation, remove (unlock) items from the cart which have not been purchased.

    Consider giving the user a 5 minute extension (secondary) reservation time after a user action that affects the cart, as well as informing the user that some items are only "reserved" until the purchase is completed. (Take a look at Airline ticket purchase sites, for instance.)

    This could be used in conjunction with one of the other methods to detect a "page unload" or "logout" depending upon business rules.

    Happy coding.