Solved

page close event help

Posted on 2004-09-23
15
1,210 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
coded character set iptc field within a jpeg 1 69
Help with sorting data in Listbox using VB.NET 3 53
how to read json value 2 30
SQL Login 17 38
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

21 Experts available now in Live!

Get 1:1 Help Now