经测试成功,其原理就是利用Anthem控件的无刷调用服务端事件。
前台页
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ToBoyUrl.aspx.cs" Inherits="ToBoyUrl" EnableViewState="False"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>关闭IE之前进行数据库操作</title>
- <script language="javascript" type="text/javascript">
- window.onbeforeunload = function()
- {
- if(document.body.clientWidth-event.clientX< 170&&event.clientY< 0||event.altKey)
- {
- Anthem_InvokePageMethod("LogOff",['system'],aa);
- }
- }
- function aa(result)
- {
- }
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- </div>
- </form>
- </body>
- </html>
后台代码
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.HtmlControls;
- using DataAccess;
- public partial class ToBoyUrl: Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- Anthem.Manager.Register(this);
- }
- [Anthem.Method]
- public string LogOff(string strID)
- {
- try
- {
- string strSql = string.Format("Update z_user set lastlogoutdate=sysdate where UNUM='system'");
- DbHelp db = new DbHelp();
- db.ExecuteNonQuery(strSql);
- db.Dispose();
- return strSql;
- }
- catch
- {
- return "失败了!";
- }
- }
- }
不过偶尔出现IE内存泄漏的情况. 希望有高手更好的解决方案