Solved

Capturing event when closing web form

Posted on 2004-08-09
10
424 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Winform Module - What is the ASP.Net equiv 2 40
Finding the IIS version 5 39
Aspx calendar pop up control 3 29
ASP.net Will we ever be able to build mobile apps in it 6 34
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

697 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