Link to home
Start Free TrialLog in
Avatar of tentavarious
tentavarious

asked on

page close event help

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.
Avatar of mAjKoL
mAjKoL

You asked the same question here:
https://www.experts-exchange.com/questions/21142400/logging-out-of-webform-problem.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.
Avatar of tentavarious

ASKER

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.
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
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.
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?
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.
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
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.
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!
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.
ASKER CERTIFIED SOLUTION
Avatar of sumo_the_cat
sumo_the_cat

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The advantages of browser-based application interface are many, but there are significant disadvantages as well!
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
The only way to make sure users do what they're supposed to do is by electrocution conditioning or similar. ;)
I suppose you could dock their pay if the session_end fires and they haven't already logged out!