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.

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 browser. Let'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 CLOSES. You 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!