Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

page close event help

Posted on 2004-09-23
15
Medium Priority
?
1,219 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
[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
  • 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
AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

688 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