关闭页面和刷新页面后 触发服务器事件 采用body的onbeforeunload()方法
onbeforeunload() 本来用来确认是否关闭页面,return confirm();
1、采用框架 加入一个隐藏页面 , 其中 <bodyonbeforeunload="lastLogin();"> 同一个页面的话点按钮postback会有问题;
2、提交服务器
<%@ WebHandler Language="C#" Class="lastLogin" %>using System;using System.Web;public class lastLogin : IHttpHandler { public void ProcessRequest (HttpContext context) { if (context.Request.Cookies["User"]["id"] != null && context.Request.Cookies["User"]["id"].ToString() != null) { string userid = context.Request.Cookies["User"]["id"].ToString(); BugLibrary.BLL.bug_user bll = new BugLibrary.BLL.bug_user(); BugLibrary.Model.bug_user user = bll.GetModel(Convert.ToInt32(userid)); if (user != null) { user.last_login = DateTime.Now; bll.Update(user); } } } public bool IsReusable { get { return false; } }}