Solved

Capturing event when closing web form

Posted on 2004-08-09
10
430 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

696 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