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

B

=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.
 
K

Kevin Spencer

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

=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.


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?
 
B

=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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top