前臺代碼:ajax
<body onbeforeunload="RunOnBeforeUnload()" class="easyui-layout" style="overflow-y: hidden" scroll="no">瀏覽器
</body>async
function RunOnBeforeUnload() {
//更改用戶登陸狀態
var currUser = getCookie("UserAccount");
$.ajax({
type: "post",
url: "../Home/UpdateLoginState",
data: { 'currUser': currUser},
dataType: "text",
async: false,
success: function (data) {
}
});
}post
後臺代碼:ui
//關閉瀏覽器後更改用戶登陸狀態
public ActionResult UpdateLoginState()
{
string currUser = "";
string stateStr = "";
if (Request["currUser"]!=null)
{
currUser = Request["currUser"].ToString();
string updaStr = "update Users set OnLine=0 where UserAccount='"+currUser.Trim()+"'";
int coun = SqlHelper.ExecuteSql(updaStr);
if (coun > 0)
{
stateStr = "更改爲功!";
}
else
{
stateStr = "更改失敗!";
}
}
return Content(stateStr);
}url