?
Solved

Capturing event when closing web form

Posted on 2004-08-09
10
Medium Priority
?
441 Views
Last Modified: 2011-10-03
Hi,


I want to cature an event that will run only when the web form is closed(ie. the window.close() method is used or the 'X' is pressed). I thought this would be the page unload event, but this runs on every postback same as page load. Ideally, what i need is the opposite of the "override protected void OnInit(EventArgs e)" method as this only runs once when the page is opened. I need to capture this in the code-behind as i need to do several db updates. Whats the best way to do this ?

Many thanks.

Christian.

Note: C# is the code behind i am using.
0
Comment
Question by:ChristianHaynes
  • 4
  • 4
8 Comments
 
LVL 9

Expert Comment

by:crescendo
ID: 11750949
Basically, you can't do it as described. Window.close and clicking the 'X' simply close the browser window, they don't cause a postback.

However, if this is the last window, it will trigger a Session_End event and you can use this to tidy up. You'll find the event handler in global.asax.

0
 

Author Comment

by:ChristianHaynes
ID: 11750967
OK thanks. The problem is this will not be the last window.
0
 
LVL 9

Expert Comment

by:crescendo
ID: 11751040
Another option would be to use the client-side onUnload event to open a another page in a new window. This page would specifically do the database updates then close itself.

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ChristianHaynes
ID: 11751493
crescendo,

Questions:

1. Does the onUnload event only fire when the window is closed ? What if i change page in the same explorer window ?
2. Do i have to open another window ? Can i not call a code-behind method, if not, can i load the window hidden ? If i load the window hidden are there any implications ?

Thanks.

0
 
LVL 9

Expert Comment

by:crescendo
ID: 11753226
The onUnload event fires for a whole host of reasons, see

http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/events/onunload.asp

Most of these you can prevent by:

1.  Not having a navigation/address bar, so that the user can't change address.
2.  Not having links on the page

But I think in general it's not a workable solution because it would get fired when you probably do a postback.

Is there some way you can alter the structure of your program so that the DB 'clutter' isn't a problem? For example, I have a shopping application and I build a table of items in the shopping cart. These are stored in the database but only get transferred to the orders table when the user clicks the button to place the order. I clear up the cart items every night using a scheduled job in SQL Server.
0
 

Author Comment

by:ChristianHaynes
ID: 11760289
Im afraid I really need to do the database updates when closing the page/form.
0
 

Author Comment

by:ChristianHaynes
ID: 11809858
Increased points due to number of replys. Anymore offers ?
0
 
LVL 9

Accepted Solution

by:
crescendo earned 1000 total points
ID: 11810837
The problem is that window.close() and clicking the "X" do not generate postbacks, so you cannot handle them server-side directly.  The events associated with closing a form (onUnload and onBeforeUnload) are also fired by normal navigation and postbacks, and there is no built-in method for detecting what fired the event, so you cannot easily use these to raise a definitive event on the server. Nor can you disable closing the window.

Your best option is to restructure your programming so that you do not wait for, or rely on, an event that you cannot control.

Failing that, you need to do something to cause a server-side event when the user closes the window. The only obvious way of doing this is to use the onUnload event to open a page in a new window (as the current window is closing) which can somehow sort out what to do, and decide whether to update the database. As I said above, this page will be opened with every postback to the main page, so it is a very inefficient way of doing what you want.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question