OnUnload event

  • Thread starter Thread starter Viktor Popov
  • Start date Start date
V

Viktor Popov

Hi,
Iwould like to do that. Is it possible because I can't do it?
in test.aspx file:
<body onunload="signout()">

in test aspx.cs
:
void private signout()
{
HttpContext.Current.Cache.Remove(Session["usrName"].ToString());
Session["user"]=null;
Session["usrName"]=null;
FormsAuthentication.SignOut();
}

Is it possible to use C# function in onunload event.
Please could you tell me hoe to implement this. I should use the code in
signout() function when onunload event occures.

Thank you,

Viktor
 
Viktor said:
Hi,
Iwould like to do that. Is it possible because I can't do it?
in test.aspx file:
<body onunload="signout()">

in test aspx.cs
:
void private signout()
{
HttpContext.Current.Cache.Remove(Session["usrName"].ToString());
Session["user"]=null;
Session["usrName"]=null;
FormsAuthentication.SignOut();
}

Is it possible to use C# function in onunload event.
Please could you tell me hoe to implement this. I should use the code in
signout() function when onunload event occures.

Thank you,

Viktor

Viktor,

You are mixing server code with client code.
QUTOE
<body onunload="signout()">
UNQUOTE

This signout() method will run in the client script not code behind.

If you want it in code behind then you can open the hidden "Web Form
Designer generated code" region and in "InitializeComponent()" method
specifically define "this.Unload += new EventHandler(resBox_Unload);"
then if you do the tabbig that VS.NET suggests, it will also generate
the unload method for you and you can type in your code.

I hope this helps.
 
Hi, Thanks for the reply!
I would like to run this code on the client side:
void private signout()
{
HttpContext.Current.Cache.Remove(Session["usrName"].ToString());
Session["user"]=null;
Session["usrName"]=null;
FormsAuthentication.SignOut();
}

How could be accomplished that?
I don't know exactly where should be written this function.
 
Viktor said:
Hi, Thanks for the reply!
I would like to run this code on the client side:
void private signout()
{
HttpContext.Current.Cache.Remove(Session["usrName"].ToString());
Session["user"]=null;
Session["usrName"]=null;
FormsAuthentication.SignOut();
}

How could be accomplished that?
I don't know exactly where should be written this function.
Viktor,

You can't do that.
1. You need to use JavaScript or VBScript on client site
2. Codebehind namespaces, sessions etc. are server objects not client.

Basically what you can do is in onunload event, call a client side
function signout, that will pop-up a window and in this window you will
just run the server side (codebehind) logic (the one you defined) and in
this pop-up windows onload event close that window with window.close().

However, as you see, this will not work all the time.

On the other hand,
1. I don't think you should keep user name in Cache object (Cache is
like Application variable, shared by everything on that application)
2. When the user closes window, his/her session will be killed anyway
and they cannot reached this session again, unless they didn't close all
related pages.

I hope this helps/
 
Thanks again,
I keep username in Cache because I would like to prevent multiple logins. Do
you know how to do that in other way? In my case everything is OK if the
user logs off from the server side button. When he/she closes the window
from X-button , she/he must wait until the session ends. That's the problem
which I should do. Could you give me an advice how to manage with that?

Thank you!
Viktor
 
Viktor said:
Thanks again,
I keep username in Cache because I would like to prevent multiple logins. Do
you know how to do that in other way? In my case everything is OK if the
user logs off from the server side button. When he/she closes the window
from X-button , she/he must wait until the session ends. That's the problem
which I should do. Could you give me an advice how to manage with that?

Thank you!
Viktor

Viktor,

I think you can also have a look at the following method

protected void Session_End(Object sender, EventArgs e)
{

}

which is located in global.asa

As you see this method happens, when the session ends. It may help.

Also, on client onunload event, you may really try to open a pop-up
window which will signout the user from the server side and then close
itself.
 
Back
Top