Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clearing Specified Session Variables When Moving to New Page

Status
Not open for further replies.

DTaylorAtWork

Technical User
Jul 12, 2007
11
CA
I am having some issues with the site I'm working on right now. It's all aspx with c# codebehind.

Most of the pages have objects of classes I have created. Those objects were becoming null on every post back so I started loading them into session variables on page_unload and then reloading them from those session variables on page_load. It looks something like this:

Code:
private myClass var1;

protected void Page_Load(object sender, EventArgs e)
{
if (Session["load"] != null)
{
var1 = (myClass)Session["var1"];
Session.Remove("load");
}
}

protected void Page_Unload(object sender, EventArgs e)
{
if (Session["load"] == null)
{
Session["var1"] = var1;
Session["load"] = true;
}
}

Now this is working, to some extent but I've just noticed that when I leave the page, or hit refresh on my browser those session variables remain so that when I return the variables get repopulated but the page is back to it's starting position.

This of course causes a problem when I start using the form with the assumption that the objects have been reset.

Is there an event that fires only when a page is navigating to a new page? That way I can clean up the session to reclaim the memory and avoid improper object values?

I'm also open to suggestions of better ways to keep the objects in the proper state without declaring them static (which causes every user to mess with the same variable and causes any number of problems).

Thanks,

Dave
 
From my understanding you can check if !IsPostback in Page_Load. That code then would be only run on the initial loading of a page.
 
That's a good idea, I can use it to clean the page specific variables on the first loading of the page. At least I won't have problem object states.

The only problem is it doesn't solve the problem of those session variables existing while browsing the rest of the site. So server memory is being taken up, a lot of it if enough people are logged in at once.
 
I'm not sure exactly what you application is doing. However, the Page_Unload will NOT fire if you navigate away from the page using an HTML control, that DOES NOT cause a postback. i.e. an HTML hyperlink.
Is there an event that fires only when a page is navigating to a new page?
No, in the Page_Load of the page navigated to, you will need to check if the session var exists and remove it if necessary. If you are going to need to do that same logic on each page, create a class, and call a fuction on that class that clears your session as necessary.
 
So if I have my page load remove the session variables after it has gathered the data from them and click a link on the page they won't exist when loading the next page.

What if I use Response.Redirect(url)? Page_Unload will still be called and I'd have to remove the session variable on page_load of the new location?
 
Yes, that's why I suggested to use a class for this. You could pass along a string, an array, generic list, etc of session variable names to be removed. You could then iterate through the list, check if they exist, if they do, remove them from session.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top