Session_end事件

简介:


Session End事件仅在进程内会话模式情况下,请求结束时,Session.Abandon()被调用时会被激发,或者会话过期时被激发。浏览器关闭并不会激发Session END事件。


Introduction

Sessions are maintained on the server. Sessions are created when a request is made by the client to the page. In this article we will look that when the Session_End event is fired.

When Session_End is fired:

Session End event is fired only in the In-Proc Session mode at the end of the request in which Session.Abandon() is called OR when the Session time expires. Session End will not be fired when you close the browserLet's make a small test in the Global.asax file so that we will know that Session.Abandon is being called.

// Here we will clear the user from the List of logged users

public static string message = String.Empty;

protected void Session_End(Object sender, EventArgs e)

{

if(Session[Global.USERNAME] == null)

{

message = null;

message = "Session End has been fired";

}

}

Now say you press a button and call Session.Abandon() method which in turns fires the Session End event.

private void Button1_Click(object sender, System.EventArgs e)

{

Session.Abandon();

Label1.Text = Global.message;

}

If you run this page you will not see any text assigned to the Label control. Even though you did assign the text in the Session End event. Well, the text is assigned alright but you need to refresh the page in order to view it since it was not included in the request which you made. When you refresh the page you will see a message "Session End has been fired".

If you want to clean the Session Variable without destroying the Session than you can use the Session.Clear() method.

In the beginning of the article I said "Session End event is fired at the end of the request in which Session.Abandon() is called". We can do a simple test to prove this. If Session_End is fired instantly when Session.Abandon is executed than the following code should kill the Session and "Session Variable has been cleared" OR an error has to be raised since that Session has been destroyed. But if you run this code it will print "Session Variable is NOT Cleared". It's a good idea if you debug it and you will see that the Session End is fired at the end of the request and not instantly when it meets Session.Abandon

Session[Global.USERNAME] = txtUserName.Text;

// Clear the Current Session

Session.Abandon();

Label1.Text = Global.message;

if(Session[Global.USERNAME] == null)

{

Label1.Text = "Session Variable has been cleared";

}

else

{

Label1.Text = "Session Variable is NOT Cleared";

}

Firing an event when the browser closes:

YOU CANNOT CLEAR SESSION WHEN THE BROWSER CLOSESYou can fire client side event when the browser closes and hence you can do client tasks. In the code below when the user closes the window I am popping up an alert message. The message will also pop whenever any activity occurs on the page. It can be button click, refresh or any other activity.

<body onbeforeunload= 'PopWindow();' >

</body>
</HTML>
<script language ="javascript">
function PopWindow() 
{
alert('You are about to close the window'); 
}
</script>

I hope you liked the article, happy coding!




本文转自斯克迪亚博客园博客,原文链接:http://www.cnblogs.com/sgsoft/archive/2008/03/13/1104265.html,如需转载请自行联系原作者

相关文章
|
存储 算法 Oracle
极致八股文之JVM垃圾回收器G1&ZGC详解
本文作者分享了一些垃圾回收器的执行过程,希望给大家参考。
|
2月前
|
机器学习/深度学习 人工智能 算法
Post-Training on PAI (4):模型微调SFT、DPO、GRPO
阿里云人工智能平台 PAI 提供了完整的模型微调产品能力,支持 监督微调(SFT)、偏好对齐(DPO)、强化学习微调(GRPO) 等业界常用模型微调训练方式。根据客户需求及代码能力层级,分别提供了 PAI-Model Gallery 一键微调、PAI-DSW Notebook 编程微调、PAI-DLC 容器化任务微调的全套产品功能。
|
SQL 数据库 Python
SqlAlchemy 2.0 中文文档(十一)(3)
SqlAlchemy 2.0 中文文档(十一)
156 11
|
Dart 监控 开发者
详细介绍Flutter Profiler的功能、使用方法以及如何利用它来提升应用的性能
【6月更文挑战第11天】Flutter Profiler是用于优化Flutter应用的关键工具,提供CPU、GPU、内存和网络分析。它帮助开发者监控运行时性能,识别瓶颈,如CPU过度使用、渲染问题、内存泄漏和网络效率低。通过选择分析类型、开始分析、查看结果,开发者可进行针对性优化。最佳实践包括定期分析、结合实际场景、关注关键指标及结合其他工具。有效利用Profiler能提升应用性能和用户体验。
480 2
|
12月前
|
存储 开发者 Windows
WINDOWS 环境变量设置方法
本文旨在帮助使用Windows电脑的开发者们为其设备配置环境变量,以更好地支持大模型应用的开发工作。文中详细介绍了三种配置方法:一是将环境变量设置为系统级变量;二是在命令行界面通过`SET`命令或`PowerShell`临时设置变量;三是借鉴MAC的方式,创建全局环境变量文件`.zshrc`进行配置。这些方法简单实用,便于根据实际需求选择适合的方式进行配置。
|
11月前
|
NoSQL Ubuntu Redis
Ubuntu安装redis
本文介绍了在Ubuntu系统上安装Redis的两种方法:一种是通过编译安装本地Redis包,包括下载、解压、编译安装、配置启动和测试连接的步骤;另一种是通过apt安装在线的Redis包,并提供了更新系统软件包列表、安装Redis服务器、检查Redis服务器状态和测试连接的命令。
1125 0
Ubuntu安装redis
|
12月前
|
机器学习/深度学习 算法 数据挖掘
从菜鸟到大师:Scikit-learn库实战教程,模型训练、评估、选择一网打尽!
【9月更文挑战第13天】在数据科学与机器学习领域,Scikit-learn是不可或缺的工具。本文通过问答形式,指导初学者从零开始使用Scikit-learn进行模型训练、评估与选择。首先介绍了如何安装库、预处理数据并训练模型;接着展示了如何利用多种评估指标确保模型性能;最后通过GridSearchCV演示了系统化的参数调优方法。通过这些实战技巧,帮助读者逐步成长为熟练的数据科学家。
381 3
|
Web App开发 JavaScript 前端开发
前端项目打包与发布
前端项目打包与发布
677 7
|
人工智能 机器人 Linux
阿里云RPA(机器人流程自动化)干货系列之四:阿里云RPA产品架构
导读:本文是阿里云RPA(机器人流程自动化)干货系列之四,详细介绍了阿里云RPA产品架构和技术架构(包括客户端和服务端)等。
8417 0
|
Java 物联网 Android开发
Android 12 蓝牙适配 Java版(下)
Android 12 蓝牙适配 Java版(下)
757 0