Session Clearing

G

Geo

Hi, I use lots of sessions. I want to clear the unwanted sessions when I
leave a page. I cant use ViewState as it will slow down the pages. Could some
one help me on this?
 
J

Jonathan Wood

By sessions, do you mean the Session collection?

If so, it's not clear to me why you'd want to clear it when leaving a page
as the only reason to use the Session collection is to store data between
page requests.

But Session items can be removed using one of several methods:

Session.Clear();
Session.RemoveAll();
Session.Remove(itemName); // Removes only the specified item
 
G

Geo

Many of them are on per page basis. I have so much data and so I dont want to
use ViewState. I save DataSets and Class objects.
 
S

sloan

One of the reasons I wrote this:
http://sholliday.spaces.live.com/blog/cns!A68482B9628A842A!125.entry

was so that I could "un-orphan" items more cleanly.
And do a .Clear() as well.

Ex:

I have a page that shows an EmployeeByDept report.

Ok...I get the data, and cache it.

So while the user is on

employeebydept.aspx
I want the page .. in session.
This allows me to sort, filter the data from session while they're on that
page, and I only hit the db one time.

When they navigate away from employeebydept.aspx page, I want to un-orphan
the data.

I call the

dataStore.Remove( MY_KEY_NAME);

and its gone.

Make sense?

Take a look, its basically a session bases Singleton object....for each
user.

I never code against the Session object directly anymore.

CHeck my blog, I have a Generic version as well, which gives you strong
typing.
 
G

Geo

Alright, I believe I am not clear here..

Every page has lots of data and i use on an average of 10 sessions a page. I
want to clear them out when I finish using them whether it is within a page
or within pages.

I could use Session.Remove, but I am afraid that I could miss some sessions.
Also where would I be calling these session.remove methods? On Page load?
Since I use menu to navigate - every screen must have these methods?

I am looking for a Page_unload event which fires when the page unloads -
EVEN when the menu is clicked. Page_Unload fires only when the current page
changes but not from the menu..

Geo.
 
S

Scott M.

This doesn't make any sense.

A session represents the time that a visitor arrives at your site to the
time they leave, regardless of what pages they visit in between. To be
creating session variables on a page and then destroying them on that same
page defeats the purpose of session.

If you only need the data on a particular page, just use regular page
variables.

If you really do need to keep data between different pages, consider using
cross-page postbacks, querystrings (if applicable), cookies, a database, or
of course sessions.

But, generally, you don't need to worry about manually clearing the session
values, as they will be dropped when the session times out (normally 20
minutes, but you can bring that number down if you like).

Just curious, why is ViewState not an option?
 
M

Mark Rae [MVP]

Every page has lots of data and i use on an average of 10 sessions a page.

I think you're getting confused with nomenclature...

When a user first visits your site, *one* session is created. There is only
*one* session per user, though each session can contain many session
variables...

Is that what you mean...?
I want to clear them out when I finish using them whether it is within a
page
or within pages.

Using session to persist data within a *single* page makes no sense at all -
that's what ViewState is for...

Session exists to allow you to persist data *across* pages...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,278
Latest member
BuzzDefenderpro

Latest Threads

Top