Click to See Complete Forum and Search --> : Using Session


srinika
February 21st, 2007, 09:48 AM
Hi,

I need an ASP.Net 2.0 application, in which, I need to write to a file the time at which each user is closing their browser.

I tried with Page_Unload --> Unsuccessful (Page_Unload is fired not at the time of closing the browser!)

Then I tried
Global.asax --> Session_End
For that I set the Web.config with the following:

......
<system.web>
<sessionState mode="InProc" cookieless="false" timeout="1" />........

Still I couldn't make it work. Please help me in this.
Also, let me know how to use session variables as we used in conventional ASP


Note : I'm OK with writing to a file, getting the server time or client time etc... and it doesn't matter whether its C# or VB 2005

Thanks
Srinika

HairyMonkeyMan
February 21st, 2007, 10:10 AM
You cannot tell when the user has closed their window. This is because the client browser would need to contact the server telling it that the window is closing.

The session_end event in global.asax fires when the session times out. You could have a java function calling another aspx page when the user closes the window i.e
<body onClose="javascript:response.redirect('clearsession.aspx');">
then in clearsession.aspx:
session.abandon();

srinika
February 21st, 2007, 10:58 AM
Ok HairyMonkeyMan,

I'm in the process of learning Session_End

How do I make it fire within a second
I set
timeout = "00:01:00" --> didn't work (Run time Error)
timeout = "0.1" --> didn't work (Run time Error)


And the other thing is:

I set the timeout = "1" and the Session_End is fired after 1 minute.

I'm trying to write some text to a file, but it goes to Application_Error
(The same code can be used in an event like button click, and it works)

The code is

Byte[] data = new Byte[100];
data = System.Text.Encoding.ASCII.GetBytes("abcd efgh");
// Failed here
System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("p.txt"), System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.Read, 8, System.IO.FileOptions.None); fs.Write(data, 0, data.Length);
fs.Flush();
fs.Close();

Thanks for your valuable info.
Srinika

srinika
February 21st, 2007, 11:12 AM
Also with regard to your answer,

I added the following in my main page (ASP.Net Source)
<body onClose="javascript:response.redirect('clearsession.aspx');">


then in clearsession.aspx:
Code:
session.abandon();

Is it a new page to be added?

And where (in which event - page_load ?) to write code ?

and one more thing:

onClose is not considered as a valid attribute

HairyMonkeyMan
February 21st, 2007, 11:14 AM
There is also a minimum timeout period for the session. I think it may be 30 seconds.

If you are wanting to write information to your file after the user leaves, I think I would save the users information in a class with a reference to the class in the session variable, just wait for the session to timeout, get the info from the class then write to the file.

Not too sure about your file writting error... maybe look into it later, but starting to get busy here, gotta run :wave:

HairyMonkeyMan
February 21st, 2007, 11:15 AM
Yes buddy,

new page, into page load