views:

256

answers:

4

I am writing an ASP.NET application that tracks the user's scores and info (it is a training application) to an access database, if any one closes the browser directly I want to display an alert message. My problem is that I cannot use the unload event because When I pressed any ASP.NET button at that time unload event occurs. What event should I be using that will work for IE and FF? I want to handle event Browser close(X).

+1  A: 

What you should be doing is having both the window unload eventhandler and the logout button click eventhandler call the same logout method. You don't need to stop the user and ask them to press your button, your button should be just another way of doing the same thing.

Also: have you considered SCORM?

annakata
+5  A: 

The onbeforeunload event will only give you the confirm box with a message. You can't put more functionality into it.

wombleton
A: 

Not sure you know what you want to do here. ;-)

Do you want to prevent the user completely from closing the browser until a certain point in the trainig? If so, have you considered using a modal and maximized window? You should be able to maximize the window without the titlebar, buttons and menus.

Or if you just want to keep track of the progress, scores or similar, then you could use cookies handled by the browser via JavaScript. Just bear in mind that this will keep the data on just one machine for the student.

If you just want to warn the user that he is about to leave the training course, you could use onbeforeunload

Tooony
A: 

I think others have adequately answered your specific question, but Tooney raises some good points. To expand on these. Where are you currently maintaining your the state? Are the scores stored in cookies, within a server-side session or do your persist them page by page within the database.

Assuming this isn't a cheap and cheerful solution, then I would suggest you consider persisting results page by page, as it is minimise the loss of information caused by a premature exit (either by design or accident). Of course, you then need a process to cleanup incomplete training session.

You could still use the onbeforeunload function to trap user exits, but personally I don't like UI's that double check users actions.

Good luck.

Jason Waring
@Jason Thanks, But I don't want to clear session, but the main difference of Browser close(X) and ASP:Button Clicked at that time I want to handle both differently.
ashish bhatt