Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

page close event help

Posted on 2004-09-23
15
Medium Priority
?
1,221 Views
Last Modified: 2009-07-08
Hello, is there a page close event, in the code behind page, similiar to a page load event, in vb.net, except it handles code when the page closes? I need some code to execute when my web form closes i was just wondering if there is a sub procedure i can use to finalize some code before the page closes?  Currently i have a logout button, but a lot of times the user does not click it and just uses the "X" in the upper right corner to close the web form, i need the code to execute in the logout button click event to execute.
0
Comment
Question by:tentavarious
  • 8
  • 6
15 Comments
 
LVL 3

Expert Comment

by:mAjKoL
ID: 12138850
You asked the same question here:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_21142400.html

and I gave you the answer. If you don't beleve me read this:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/aspnet-pageobjectmodel.asp

Especially "The Page Lifecycle" part.
0
 

Author Comment

by:tentavarious
ID: 12142402
I tried what you said  and I couldn't get the code to execute, i was hoping to get some more input if i changed the question.
0
 

Author Comment

by:tentavarious
ID: 12142977
How come this procedure doesnt execute when the page is close?
  Private Sub Page_Unload(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Unload
        If Session("LoggedOut") = "true" Then
            lbltimehold.Text = TimeOfDay

            If checkdate() = False Then
                Exit Sub
            End If
            Timelogout()
            If lblTothours.Text = lblHours.Text Then
                'if the hours equal records get added to jerry's table and there session is abandoned
                AddEmptimes()
                logout()
                Response.Cookies("mess1").Value = "You have completed your TimeCard Entries for " + txtDate.Text + "."
                Response.Cookies("mess2").Value = "Your total hours for today were" + " " + lblHours.Text
                Session.Abandon()
                Response.Redirect("successlogout.aspx")
            Else
                'if hours do not equal record is not added, and the user is prompted to log back in later
                logout()
                Response.Cookies("mess1").Value = "Your TimeCard hours do not match your total hours worked for" + " " + txtDate.Text + "." + " Please log back in and finish filling out your TimeCards sometime today."
                Response.Cookies("mess2").Value = "Your total hours for today were" + " " + lblHours.Text
                Session.Abandon()
                Response.Redirect("successlogout.aspx")
            End If
        Else
            Exit Sub
        End If

    End Sub
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 2

Expert Comment

by:sumo_the_cat
ID: 12143189
How is the server supposed to know when the page is closed? I think you need to rethink your general design. It shouldn't depend on knowing that the user has closed a particular page.
0
 

Author Comment

by:tentavarious
ID: 12143257
The Unload event works fine when the page is closed within the page, but if the user closes the page by closing the browser the Unload event will not fire.  How else can i get around it.  The user is suppose to use the logout button to end his session and send some data to a database.  What i am doing is keeping track of the login and logout times, when the user logs in the time and date is recorded and when the user logs out that time is recorded, this only works if the log out button is clicked, so is there a way i can force them to only exit the page by clicking the button?
0
 
LVL 2

Expert Comment

by:sumo_the_cat
ID: 12143413
Exactly. The web is STATELESS - there is no connection held open between web requests. This is the cause of your problem. There is no way for the server to know that the user has closed their brower, turned off their computer, etc., because of this fact. Therefore you cannot rely on this in your application. There is no way around this. There are other problems in designing web-based database apps that are a direct result of this stateless nature - ever accidentally purchased, or otherwise submitted, something twice on the internet for example? There is no way around this problem  and that is the answer unfortunately!
Peter.
0
 

Author Comment

by:tentavarious
ID: 12143558
I know there is some javascripting i can do that will execute if the browser is closed, i am able to popup a confirmation box
but that will execute everytime the page is closed or a link is clicked.  Maybe there is someone that i can set cookie and check it if exists if so dont popup the confirmation box.  It will have to be in javascript
0
 
LVL 2

Expert Comment

by:sumo_the_cat
ID: 12143563
The only thing you could rely on and monitor is the end of the Session. There is an event in the Global.asax which fires when the user's session ends, but this is 20 minutes (or whatever time is set in Web.Config) after their last activity.
0
 
LVL 2

Expert Comment

by:sumo_the_cat
ID: 12143581
Even with Javascript, they could turn their computer off or just leave the page open for ages until long after their session expires - you still can't rely on it!
0
 

Author Comment

by:tentavarious
ID: 12144159
Ok so if i add the code into the session end procedure on the global.aspx, the code will execute if the page is idle for over 20 minutes, is this correct?  This is another issue because the user will sometimes leave the page open and walk away, so that would solve that issue correct.
0
 
LVL 2

Accepted Solution

by:
sumo_the_cat earned 1000 total points
ID: 12144275
Yes, the event will fire when the session ends. How long this is since the last request depends on the timeout value in the sessionState part of the Web.Config . It's normally 20 or 30 minutes - i don't remember what the default is. It's always annoying to find out that something cannot be done, but you really need to understand the architecture of the web model before trying to use it - i know this now from experience! There are lots of other unexpected things for the asp.net novice, in particular all the craziness surrounding the way the viewstate works.
0
 
LVL 2

Expert Comment

by:sumo_the_cat
ID: 12144296
The advantages of browser-based application interface are many, but there are significant disadvantages as well!
0
 

Author Comment

by:tentavarious
ID: 12144596
Well i guess i will try the session end, and i will have to find a new way of making sure they log out. thanks for the help
0
 
LVL 2

Expert Comment

by:sumo_the_cat
ID: 12144657
The only way to make sure users do what they're supposed to do is by electrocution conditioning or similar. ;)
0
 
LVL 2

Expert Comment

by:sumo_the_cat
ID: 12144679
I suppose you could dock their pay if the session_end fires and they haven't already logged out!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
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…

971 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