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

这篇文章给大家介绍ASP.NET如何关闭页面服务端及对话层清空,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

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

window.onbeforeunload = function()   {      //这是网上找的,具体没验证过
  •       var n = window.event.screenX - window.screenLeft;   

  •       var b = n > document.documentElement.scrollWidth-20;   

  •       if(b && window.event.clientY < 0 || window.event.altKey)     

  •       {     

  •           ClearSession();   

  •       }     

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

========================Default.aspx 开始===========================================   
<%@ Page Language="C#" AutoEventWireup="true" 
CodeFile="Default.aspx.cs" Inherits="_Default" %>     
<!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>无标题页</title>         
<script type="text/javascript" src="script.js"></script>     
</head>  
<body>      
<form id="form1" runat="server">          
<div>              
<asp:Label ID="Label1" runat="server"></asp:Label>              
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="写入Session" /></div>      
</form>  
</body>  
</html>  
========================Default.aspx 结束===========================================   
========================Default.aspx.cs 开始===========================================   
using System;  
using System.Data;  
using System.Configuration;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Web.UI.HtmlControls;     
public partial class _Default : System.Web.UI.Page  {      
protected void Page_Load(object sender, EventArgs e)      {                    
if (!string.IsNullOrEmpty(Request.QueryString["___command"]))          {              
string cmd = Request.QueryString["___command"];              
if (cmd == "ClearSession")                  
Session.Remove("name");//清空Session          
}             
if (Session["name"] != null)              
this.Label1.Text = Session["name"].ToString();      
}      protected void Button1_Click(object sender, EventArgs e)      
{          Session["name"] = "vvvvvvvvvvvvv";          
if (Session["name"] != null)              
this.Label1.Text = Session["name"].ToString();      
}  
}     
========================Default.aspx.cs 结束===========================================      
========================script.js 开始===========================================   
function GetXmlHttpObject()   {      
//创建XMLHttpRequest对象来发送和接收HTTP请求与响应     
 xmlHttpObj = null;      try       {          
 // FireFox Opera 8.0+ Safari          
 xmlHttpObj = new XMLHttpRequest();          
 if(xmlHttpObj.overrideMimeType)          
  {              
  xmlHttpObj.overrideMimeType('text/xml');         
   }     
  }      
   catch(e)       {          
   // IE          try           {              
   xmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");         
    }           
    catch(e)           
    {             
     xmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");         
      }     
       }      
       return xmlHttpObj; 
        }    
         function StateChanged()   {      
         if(___xmlHttp.readyState == 4)       {           
         if(___xmlHttp.status == 200)           {          
   }           else           {         
             }      
          }  
 }     
          var ___xmlHttp=null; 
   function ClearSession()  
   {     
             if(___xmlHttp==null)    
                   ___xmlHttp = GetXmlHttpObject();    
  if(___xmlHttp == null)        
                        return false;              
 var url = "?___command=ClearSession&___clientRandom=" + Math.random();         
 ___xmlHttp.open("GET", url, true);      
 ___xmlHttp.onreadystatechange = StateChanged;      
 ___xmlHttp.send(null);        }     
 window.onbeforeunload = function()   {          
  var n = window.event.screenX - window.screenLeft;      
     var b = n > document.documentElement.scrollWidth-20;     
         if(b && window.event.clientY < 0 || window.event.altKey)   
   {               ClearSession();       
   }   
 }


关于ASP.NET如何关闭页面服务端及对话层清空就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

评论

有免费节点资源,我们会通知你!加入纸飞机订阅群

×
天气预报查看日历分享网页手机扫码留言评论电报频道链接