Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

do some action on page_unload

Status
Not open for further replies.

bclt

Programmer
Mar 13, 2005
363
GR
Hi,

Look at my code:

Code:
    Private Sub Out()
        File.Delete(Server.MapPath("./History/") & txtName.Text & ".txt")
        FormsAuthentication.SignOut()
    End Sub

    Private Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Unload
        Out()
    End Sub

All i want is when the user closes the form WITHOUT signing off, to call the Out() routine to delete a file from the server and then FormsAuthentication.SignOut().

If i add a button which calls the Out() everything works; by adding the Out() the the page_unload, the Out() routine is NOT called.


What's wrong ?

Tnx
 
by adding the Out() the the page_unload, the Out() routine is NOT called.
The Out() Sub will be called when the page unloads. What I think you are getting confused about is when the unload event is called. What happens is the page initiates, loads and then unloads so it will be called after the page load event.

It won't be closed, for example, if the user just closes their browser as that page has already been loaded nad unloaded and is not just sat as on the client as rendered HTML. How would the server know that the client has closed the browser as nothing is sent to the server?

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
if the user just closes their browser as that page has already been loaded nad unloaded and is not just sat as on the client as rendered HTML
Sorry that should have read:

if the user just closes their browser as that page has already been loaded and unloaded and is just sat as on the client as rendered HTML

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
so what should i do? By closing the page to signout and delete the file i need to .
 
Find some other way other than the unload event. You can't rely on it being fired. You might try a timer.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
Chip H,

In the page there is a textfield where you put your username, and a login button.

By pressing the login button and if username is correct you are redirected in an other page. In this page there is the logout button. When pressed two things happen: 1.delete a file from the server and 2. FormsAuthentication.SignOut() !

Note that I must delete a file from the server so, ... If by mistake click "x" (top-right) the user will be automatically loged out BUT the file WON'T be deleted :(

If i make the routine "Out()" that does the two things mentioned above and put it to be called n Page_unload, straingless nothing will be done.

 
Sorry, but that's how web apps work. There is no way to know when a user leaves a page, whether it's by them closing the browser, losing network connectivity, or their PC dies.

What I've always seen done is encode the file by date-time (whether in the filename itself, or in a directory name), and a couple of days later, have an automated service delete the old files.

Chip H.


____________________________________________________________________
Click here to learn Ways to help with Tsunami Relief
If you want to get the best response to a question, please read FAQ222-2244 first
 
You could have a javascript event that fires when a browser window closes that would open up another aspx page that handles the delete but bear in mind that if a window opens when I click the close button on my browser I quickly close it and get annoyed! (also popup blockers may stop it).

The main point here though is what chiph points out - thats how web apps work!

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Is this something that you could put in the global.asax file?

Code:
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
  ' Fires when the session ends
End Sub

I've never used this, but i have read a little about it...

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
To do that you will have to have a client side script that will interact with your code behind. There is no way to tell from the code behind when a user clicks the "X" becouse that is a client side function. However you can do something like this...

in the body tag add this to every page:

onunload="javascript:UnloadForm();"


then directly under your server form tag put this (note that you must have a public bool userIsLoggedOn to tell the javascript if a user is logged on or not):

<input type="hidden" id="txt_loggedOn" value="<%=userIsLoggedOn.ToString()%>">
<input type="hidden" id="txt_fireEvent" value="yes">


in your header you must put the following script:

<script language="javascript">
function UnloadForm()
{
if(txt_loggedOn.value=="true")
{
if(txt_fireEvent.value=="yes")
{
window.open("deleteFilePage.aspx");
}
}
}
function DisableEvent()
{
txt_fireEvent.value="no";
}
</script>

okay...
now to use this you have to make a new page called "deleteFilePage.aspx" and make this page delete the server side file you need to delete on the page load. also i would make some cool effect like have that page close itself right after the server side script runs.

most importantly though you will have to call javascript:DisableEvent(); before every post back and every page redirect becouse if you dont it will pop up your new form ("deleteFilePage.aspx") and the file will be deleted before you want it to.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top