ASP.NET 关闭页面服务端对话层清空

开发 后端
在客户端改变服务端的内容部可能实现,本文为大家介绍下ASP.NET 关闭页面清除服务端Session的方法,希望对您有所帮助。

      要清掉Session必须回到服务端,在客户端是不能改变服务端内容的。ASP.NET 关闭页面服务端对话层的清空我们可以变通下——使用ajax。首先我们要判断用户什么时候关闭了页面,这样才能执行下一步动作。不过HTML DOM没要页面关闭的事件,只有onunload和onbeforeunload是与ASP.NET 关闭页面有关的,ASP.NET 关闭页面或刷新后的事件,onbeforeunload是ASP.NET 关闭页面或刷新前的事件,所以我们要用的是onbeforeunload。要判断下用户是关闭页面还是在刷新页面。代码如下:

  1. window.onbeforeunload = function()   
  2. {     
  3.  //这是网上找的,具体没验证过  
  4.       var n = window.event.screenX - window.screenLeft;   
  5.       var b = n > document.documentElement.scrollWidth-20;   
  6.       if(b && window.event.clientY < 0 || window.event.altKey)     
  7.       {     
  8.           ClearSession();   
  9.       }     

ClearSession()为ajax调用请求服务端,服务端接收到请求后执行清空Session的操作。Ajax的东西不多说了,下面为代码。

  1. ========================Default.aspx 开始===========================================  
  2.  
  3. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  4.    
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  6. <html xmlns="http://www.w3.org/1999/xhtml">  
  7. <head runat="server">  
  8.     <title>无标题页</title>  
  9.    
  10.     <script type="text/javascript" src="script.js"></script>  
  11.    
  12. </head>  
  13. <body>  
  14.     <form id="form1" runat="server">  
  15.         <div>  
  16.             <asp:Label ID="Label1" runat="server"></asp:Label>  
  17.             <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="写入Session" /></div>  
  18.     </form>  
  19. </body>  
  20. </html>  
  21. ========================Default.aspx 结束===========================================  
  22.  
  23.  
  24. ========================Default.aspx.cs 开始===========================================  
  25.  
  26. using System;  
  27. using System.Data;  
  28. using System.Configuration;  
  29. using System.Web;  
  30. using System.Web.Security;  
  31. using System.Web.UI;  
  32. using System.Web.UI.WebControls;  
  33. using System.Web.UI.WebControls.WebParts;  
  34. using System.Web.UI.HtmlControls;  
  35.    
  36. public partial class _Default : System.Web.UI.Page  
  37. {  
  38.     protected void Page_Load(object sender, EventArgs e)  
  39.     {  
  40.           
  41.         if (!string.IsNullOrEmpty(Request.QueryString["___command"]))  
  42.         {  
  43.             string cmd = Request.QueryString["___command"];  
  44.             if (cmd == "ClearSession")  
  45.                 Session.Remove("name");//清空Session  
  46.         }  
  47.    
  48.         if (Session["name"] != null)  
  49.             this.Label1.Text = Session["name"].ToString();  
  50.     }  
  51.     protected void Button1_Click(object sender, EventArgs e)  
  52.     {  
  53.         Session["name"] = "vvvvvvvvvvvvv";  
  54.         if (Session["name"] != null)  
  55.             this.Label1.Text = Session["name"].ToString();  
  56.     }  
  57. }  
  58.    
  59. ========================Default.aspx.cs 结束===========================================  
  60.  
  61.  
  62.  
  63.  
  64. ========================script.js 开始===========================================   
  65. function GetXmlHttpObject()   
  66. {  
  67.     //创建XMLHttpRequest对象来发送和接收HTTP请求与响应  
  68.     xmlHttpObj = null;  
  69.     try   
  70.     {  
  71.         // FireFox Opera 8.0+ Safari  
  72.         xmlHttpObj = new XMLHttpRequest();  
  73.         if(xmlHttpObj.overrideMimeType)   
  74.         {  
  75.             xmlHttpObj.overrideMimeType('text/xml');  
  76.         }  
  77.     }   
  78.     catch(e)   
  79.     {  
  80.         // IE  
  81.         try   
  82.         {  
  83.             xmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");  
  84.         }   
  85.         catch(e)   
  86.         {  
  87.             xmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");  
  88.         }  
  89.     }  
  90.     return xmlHttpObj;  
  91. }  
  92.    
  93. function StateChanged()   
  94. {  
  95.     if(___xmlHttp.readyState == 4)   
  96.     {   
  97.         if(___xmlHttp.status == 200)   
  98.         {  
  99.         }   
  100.         else   
  101.         {  
  102.         }  
  103.     }  
  104. }  
  105.    
  106. var ___xmlHttp=null;  
  107. function ClearSession()   
  108. {  
  109.     if(___xmlHttp==null)  
  110.         ___xmlHttp = GetXmlHttpObject();  
  111.     if(___xmlHttp == null)   
  112.         return false;  
  113.           
  114.     var url = "?___command=ClearSession&___clientRandom=" + Math.random();  
  115.    
  116.     ___xmlHttp.open("GET", url, true);  
  117.     ___xmlHttp.onreadystatechange = StateChanged;  
  118.     ___xmlHttp.send(null);  
  119.       
  120. }  
  121.    
  122. window.onbeforeunload = function()   
  123. {     
  124.       var n = window.event.screenX - window.screenLeft;   
  125.       var b = n > document.documentElement.scrollWidth-20;   
  126.       if(b && window.event.clientY < 0 || window.event.altKey)     
  127.       {     
  128.           ClearSession();   
  129.       }     
  130. }   

本文来自:博客           作者:陈粤雄

【编辑推荐】

  1. 中途关闭ASP.NET是否影响服务器端执行
  2. 关闭浏览器Session1分钟失效ASP.NET的BUG
  3. 概述ASP.NET页面框架
  4. 描述ASP.NET页面表单
  5. ASP.NET 页面对象模型
责任编辑:林琳 来源: Vincent's Blog
相关推荐

2009-07-23 14:17:41

2009-07-29 17:26:39

ASP.NET页面

2009-07-31 10:23:44

缓存页面ASP.NET缓存

2009-09-03 18:37:35

ASP.net

2009-08-03 13:38:18

ASP.NET编程模型

2009-07-29 14:35:34

页面输出缓存ASP.NET

2009-07-31 10:33:54

ASP.NET页面输出

2009-07-23 10:52:38

2009-08-05 18:22:55

2009-07-27 15:25:40

aspx页面ASP.NET

2009-07-28 16:40:11

ASP.NET异步页面

2009-07-23 14:21:55

ASP.NET页面

2009-07-21 15:40:59

Asp.Net动态页面

2009-08-19 10:54:42

ASP.NET数据访问

2009-07-27 12:56:27

控件CheckBoxLASP.NET服务器

2009-07-23 14:08:58

2009-07-31 13:06:53

CheckBoxLisASP.NET页面

2009-08-05 14:01:50

ASP.NET配置错误

2009-07-29 16:41:45

ASP.NET页面框架

2009-07-27 17:54:39

WCF服务ASP.NET
点赞
收藏

51CTO技术栈公众号