Solved

page close event help

Posted on 2004-09-23
15
1,209 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 2

Expert Comment

by:sumo_the_cat
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
The advantages of browser-based application interface are many, but there are significant disadvantages as well!
0
 

Author Comment

by:tentavarious
Comment Utility
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
Comment Utility
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
Comment Utility
I suppose you could dock their pay if the session_end fires and they haven't already logged out!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

7 Experts available now in Live!

Get 1:1 Help Now