By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,092 Members | 1,546 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,092 IT Pros & Developers. It's quick & easy.

Trapping the <BODY> onUnload() / Detecting when a user closes the browser

P: n/a
=B=
Hi all,

I was wondering if anyone has had any luck with trapping the <BODY>
onUnload() event in ASP.NET?

The thing is, I'm writing code for an Intranet site. The code makes a
call to a third-party component (think of it as a lock) when the page
loads. Now the OK and Cancel server buttons I can handle and use to
release the lock as required. But previously in classic ASP I had
been trapping the user hitting the close 'x' button by using
JavaScript in the onUnload() event of the <BODY> tag. This JavaScript
would submit a form to a dedicated page which would retrieve the form
values and release the specified lock.

Does anyone know if it's possible to trap this with ASP.NET alone?
I've tried adapting my old classic ASP way but it gets really messy
with the postbacks causing an onUnload() to fire, etc.

Any help would be greatly appreciated.

Thanks,

B.
Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
You can do anything with ASP.Net that you can do with ASP. In fact, you can
do much, much more. However, ASP.Net isn't nearly as easy to use as ASP, in
fact, much, much more complex. Think of ASP as a single-engine small
aircraft. Think of ASP.Net as the Space Shuttle. Almost anyone can pilot a
small aircraft with only a few dozen hours of training. It takes years to
train a pilot of the Space Shuttle. But of course, the Space Shuttle can go
much farther and faster than the small aircraft.

I'm having aproblem with your design, though. Counting on the client browser
triggering a server-side event on close is unreliable at best. What if, for
example, the client never closes their browser? What if the client machine
has a problem and has to reboot? What if the client's Internet Connection is
lost? How important is it for you to execute these operations? What are the
consequences if one client does not? What about 2 clients? What would the
cumulative effect be of this happening over a long period of time, such as
memory leaks, unclosed database connections, locks not released, etc.? What
you have here is an unreliable external dependency.

IMHO, you're better off using the Session_End event handler for this. It
ALWAYS fires eventually.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"=B=" <br*************@yahoo.co.uk> wrote in message
news:46**************************@posting.google.c om...
Hi all,

I was wondering if anyone has had any luck with trapping the <BODY>
onUnload() event in ASP.NET?

The thing is, I'm writing code for an Intranet site. The code makes a
call to a third-party component (think of it as a lock) when the page
loads. Now the OK and Cancel server buttons I can handle and use to
release the lock as required. But previously in classic ASP I had
been trapping the user hitting the close 'x' button by using
JavaScript in the onUnload() event of the <BODY> tag. This JavaScript
would submit a form to a dedicated page which would retrieve the form
values and release the specified lock.

Does anyone know if it's possible to trap this with ASP.NET alone?
I've tried adapting my old classic ASP way but it gets really messy
with the postbacks causing an onUnload() to fire, etc.

Any help would be greatly appreciated.

Thanks,

B.

Nov 18 '05 #2

P: n/a
=B=
br*************@yahoo.co.uk (=B=) wrote in message news:<46**************************@posting.google. com>...
Hi all,

I was wondering if anyone has had any luck with trapping the <BODY>
onUnload() event in ASP.NET?

The thing is, I'm writing code for an Intranet site. The code makes a
call to a third-party component (think of it as a lock) when the page
loads. Now the OK and Cancel server buttons I can handle and use to
release the lock as required. But previously in classic ASP I had
been trapping the user hitting the close 'x' button by using
JavaScript in the onUnload() event of the <BODY> tag. This JavaScript
would submit a form to a dedicated page which would retrieve the form
values and release the specified lock.

Does anyone know if it's possible to trap this with ASP.NET alone?
I've tried adapting my old classic ASP way but it gets really messy
with the postbacks causing an onUnload() to fire, etc.

Any help would be greatly appreciated.

Thanks,

B.

For more information, I can trap the window_onUnload() when it's
attached to the <BODY> tag and call __doPostback to call the same code
as the Cancel button, i.e. with this javascript:

function window_onUnload()
{
// If a control hasn't caused a postback...
__doPostBack('btnCancel','');
}

The problem with this is that I have controls which also cause
postbacks. Is there any way to distinguish this client-side?
Nov 18 '05 #3

P: n/a
=B=
Kevin,

Thanks for the reply, but this site is essentially sitting on top of a
workflow engine which manages queues of work assigned to users. Users
can 'take' activities from their queue, and complete or cancel them.
My page to handle user's taking activities is a popup window and I
feel the temptation will be for users to close this window down (when
they wish to cancel the activity) rather than hit the Cancel button
provided, which has code to actually cancel it from the perspective of
the workflow engine.

Regarding users getting disconnected or rebooting, the workflow engine
has inbuilt mechanisms for handling these. For example, if an
activity hasn't been completed 15 minutes after being taken it is
cancelled. If a user logs off and on again, any taken activities are
cancelled.

I know I can't handle everything, but I was able to trap closing the
browser window in classic ASP so I'd also like to be able to do it in
..NET.

Thanks,

B.

"Kevin Spencer" <ks******@takempis.com> wrote in message news:<O3**************@TK2MSFTNGP15.phx.gbl>...
You can do anything with ASP.Net that you can do with ASP. In fact, you can
do much, much more. However, ASP.Net isn't nearly as easy to use as ASP, in
fact, much, much more complex. Think of ASP as a single-engine small
aircraft. Think of ASP.Net as the Space Shuttle. Almost anyone can pilot a
small aircraft with only a few dozen hours of training. It takes years to
train a pilot of the Space Shuttle. But of course, the Space Shuttle can go
much farther and faster than the small aircraft.

I'm having aproblem with your design, though. Counting on the client browser
triggering a server-side event on close is unreliable at best. What if, for
example, the client never closes their browser? What if the client machine
has a problem and has to reboot? What if the client's Internet Connection is
lost? How important is it for you to execute these operations? What are the
consequences if one client does not? What about 2 clients? What would the
cumulative effect be of this happening over a long period of time, such as
memory leaks, unclosed database connections, locks not released, etc.? What
you have here is an unreliable external dependency.

IMHO, you're better off using the Session_End event handler for this. It
ALWAYS fires eventually.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"=B=" <br*************@yahoo.co.uk> wrote in message
news:46**************************@posting.google.c om...
Hi all,

I was wondering if anyone has had any luck with trapping the <BODY>
onUnload() event in ASP.NET?

The thing is, I'm writing code for an Intranet site. The code makes a
call to a third-party component (think of it as a lock) when the page
loads. Now the OK and Cancel server buttons I can handle and use to
release the lock as required. But previously in classic ASP I had
been trapping the user hitting the close 'x' button by using
JavaScript in the onUnload() event of the <BODY> tag. This JavaScript
would submit a form to a dedicated page which would retrieve the form
values and release the specified lock.

Does anyone know if it's possible to trap this with ASP.NET alone?
I've tried adapting my old classic ASP way but it gets really messy
with the postbacks causing an onUnload() to fire, etc.

Any help would be greatly appreciated.

Thanks,

B.

Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.