Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am inserting user details when user login in my website.

At the time of user logout then i have to update logout status .

if user logout properly i can catch the status as logout but if user close the browser without click logout button then how can i catch the user has logout??

Please help me how to solve this problem
Posted
Comments
anushripatil 30-Dec-11 6:10am    
check out the links
http://forums.asp.net/t/1220828.aspx/1
http://www.jguru.com/faq/view.jsp?EID=34927
http://www.codeproject.com/Tips/154801/How-to-end-user-session-when-browser-closed

Hi...

This is a very critical problem in asp.net.

There is a one solution to call your logout function.

your can call a javascript function on browsers close event.
This is a event which get called before browser closed

XML
<script language="javascript" type="text/javascript">
    function unloadfunction() {
        //Call your server side function using javascript.
    }
  </script>



<body  onbeforeunload="unloadfunction()">


Using this javascript function you can call server side logout function.

And the second way is call logout function in global.asax file.

This event get fired when session get end.
Write down this type of code in global.asax file.

void Session_End(object sender, EventArgs e)
  {
      SqlCommand cmd = new SqlCommand();
      Loader ld = new Loader();
      try
      {
          ld.Openconnection();
          cmd = new SqlCommand("DeleteOnlineUser", ld.con);
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.Parameters.AddWithValue("@UserName", Convert.ToString(Session["UserName"]));
          cmd.ExecuteNonQuery();
      }
      catch (Exception ex)
      {
          ex.ToString();
      }
      finally
      {
          ld.Closeconnection();
      }

  }


Here i have pass user name to database.
When user click on logout then i am deleting this username from database table.


If you have any problem regarding this then you can contact me on this email.
 
Share this answer
 
v2
Hi thanks for help ,,

The problem it is not work with me :(

I need to write code behind when the user close the browser from close button in the top (x)
any idea ??
 
Share this answer
 
Comments
CHill60 3-Oct-13 12:11pm    
If you have a question of your own use the "Ask a question" link, don't post a question as a solution to an old post - very few people will see it
salah9 15-Oct-13 5:01am    
from client-side you can call your server-side method with __dopostback event
HK33 26-May-14 4:28am    
can u please show an example?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900