Solved

Capturing event when closing web form

Posted on 2004-08-09
10
414 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

831 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