Home > Software Development > Clearing Session on window close Part 1

Clearing Session on window close Part 1

Dealing with an app that needs to store quite a bit of data and Session as been chosen throughout the app.  However, this makes me nervous as the traffic against the application grows.  For the pages I’m writing, I’ve been searching for a way to clear the session variables when a use goes to another page or closes IE.
 
I finally got what I was looking for using ajax WCF service in my application.
 
My default.aspx page is as follows:
 

<%@ 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"&gt;

<

html xmlns="http://www.w3.org/1999/xhtml"&gt;

<

head runat="server">

<title></title>

</

head>

<

body>

<form id="form1" runat="server">

<script type="text/javascript">

function window.onunload() {

var checked = $get(‘<%= CheckBox1.ClientID %>’).checked;

// Use window.event.clientY values to determine when you want to clear session

//alert(window.event.clientY);

if (checked) {

SessionMethods.ClearSession(ClearSuccess, ClearFail);

}

}

// These won’t fire because the window is gone

function ClearSuccess(){

alert(

‘ClearSuccess’);

}

function ClearFail(){

alert(

‘ClearFail’);

}

</script>

<asp:ScriptManager ID="ScriptManager1" runat="server">

<Services>

<asp:ServiceReference Path="~/SessionMethods.svc" />

</Services>

</asp:ScriptManager>

<div>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">

<ContentTemplate>

<div>

<asp:Label ID="Label2" runat="server" Text="Session DateTime:"></asp:Label>

</div>

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

<div>

<asp:CheckBox ID="CheckBox1" runat="server" Text="Clear Session on window.unload? Checked, Session state is cleared when you leave the page. Unchecked Session is left in tact."></asp:CheckBox>

</div>

<div>

<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

</div>

</ContentTemplate>

</asp:UpdatePanel>

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/OtherPage.aspx">HyperLink</asp:HyperLink>

</div>

</form>

</

body>

</

html>

 

The code behind for this page page is.

using

System;

using System.Collections.Generic;

using

System.Linq;

using

System.Web;

using

System.Web.UI;

using System.Web.UI.WebControls;

public

partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

Label1.Text = Session[

"SessionTest"] as string;

}

protected void Button1_Click(object sender, EventArgs e)

{

Session[

"SessionTest"] = DateTime.Now.ToString();

Label1.Text = Session["SessionTest"] as string;

}

}

 

Advertisements
Categories: Software Development
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: