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
-
1It is very difficult to call code when someone leaves a page and get it to work correctly. – mellamokb Apr 02 '12 at 16:42
-
Possible duplicate of http://stackoverflow.com/questions/5860926/how-to-determine-if-user-left-our-site – MilkyWayJoe Apr 02 '12 at 16:42
-
So the user adds items to their cart, and if they leave the page without saving you want to remove the items from the cart? Couldn't you just wait to add the items until the user clicks the save button? – James Johnson Apr 02 '12 at 16:45
-
possible duplicate of [Display a warning when leaving the site, not just the page](http://stackoverflow.com/questions/2365994/display-a-warning-when-leaving-the-site-not-just-the-page) – John Saunders Apr 02 '12 at 16:47
-
problam is that this is company "inside" program, when one user selling items, other can't sell the same items – Ignas Apr 02 '12 at 16:48
4 Answers
You could put a handler on Session_End in your global.asax and do a check there releasing whatever was reserved.

- 20,171
- 12
- 62
- 114
-
Please note that this event doesn't happen in all deployment scenarios... I don't remember the exact details but it depends on how your app domain is hosted in IIS. – RQDQ Apr 02 '12 at 16:46
-
2Keep in mind there are situations where Session_End won't get called. This also doesn't cover the user leaving the page so the item(s) will remain locked until it times out. – Kris Apr 02 '12 at 16:47
-
2The problem with this solution is that `Session_End` is not guaranteed to be called. Unfortunately, you can't rely too heavily on the events in the `global.asax`. – James Johnson Apr 02 '12 at 16:47
-
I am not so good in English, but of all your comments, I think, that first saved order wins and othes loses :) – Ignas Apr 02 '12 at 16:51
-
-
@Ignas - In my opinion, the first person to save the order means that you shouldn't "reserve" anything until the save button is clicked. That gets ride of this whole discussion about needing to release reservations. – Code Maverick Apr 02 '12 at 16:54
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.
Sample HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script language="javascript" type="text/javascript">
window.onbeforeunload = function (evt) {
debugger;
if (confirm('Are you sure to exit')) {
document.getElementById('<%=btn.ClientID %>').click();
}
else
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btn" runat="server" Text="Click for SP" OnClick="btn_Click" />
</div>
</form>
</body>
</html>
Sample Code Behind
protected void btn_Click(object sender, EventArgs e)
{
//SP Call
}
Why are you going to the database for intermediate operations? Why don't you save the cart in session before user presses save button ? This can cause performance issue in your application. Can you afford to go to database on each item click for order ?

- 9,749
- 32
- 139
- 283
-
Then use Window->Close or File->Exit (or crash/force-kill the browser) ... if it works or not depends on browser. – Apr 02 '12 at 17:08
-
-
-
-
That is the "best" way to do that. Just be aware that not all things that close the window will trigger that [nonstandard] event. – Apr 02 '12 at 17:34
Why not try a long poll, you could raise a connection with a token specific to a user, once the connection is dropped and not raising again within a short timeframe, you could class the user as left the site.

- 7,456
- 9
- 50
- 80