如何在c#.net中关闭浏览器时捕获用户注销状态 [英] how to catch user logout status when browser is closed in c#.net

查看:59
本文介绍了如何在c#.net中关闭浏览器时捕获用户注销状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在用户登录网站时插入用户详细信息。



At用户注销的时间,然后我必须更新注销状态。



如果用户正常注销我可以捕获状态注销但是如果用户关闭浏览器没有单击注销按钮然后我怎么能抓住用户退出?



请帮我解决这个问题

解决方案

< blockquote>嗨...



这是asp.net中一个非常关键的问题。



那里是一个调用你的注销功能的解决方案。



你可以在浏览器关闭事件上调用javascript函数。

这是一个事件在浏览器关闭之前调用



 <   script     language   =  javascript   类型  =  text / javascript >  
function unloadfunction(){
//使用javascript调用服务器端函数。
}
< / script >







< body onbeforeunload = unloadfunction()> 





使用这个javascript函数可以调用服务器端注销功能。



第二种方式是global.asax文件中的调用注销功能。



当会话结束时会触发此事件。

在global.asax文件中记下这种类型的代码。



 void Session_End(object sender,EventArgs e)
{
SqlCommand cmd = new SqlCommand();
Loader ld = new Loader();
尝试
{
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();
}
最后
{
ld.Closeconnection();
}

}





这里我将用户名传递给数据库。

当用户点击退出时,我将从数据库表中删除此用户名。





如果你有有关于此的任何问题,那么你可以通过这封电子邮件与我联系。


您好,感谢您的帮助,



问题是不能和我合作:(



当用户从顶部的关闭按钮关闭浏览器时我需要编写代码(x)

任何想法??


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

解决方案

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

<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.


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 ??


这篇关于如何在c#.net中关闭浏览器时捕获用户注销状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆