Solved

Capturing event when closing web form

Posted on 2004-08-09
10
404 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
10 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 250 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now