Help with transferring Session object between pages.

E

ESmith

I have multi-page form where I pass an object between pages as follows:

On each page:

// What page to go to next
private void btnContinue_Click(object sender, System.EventArgs e)
{
if (Page.IsValid)
{
MyXferObj obj = new MyXferObj();
obj.dataitemX = somevalue;
obj.dataitemY = somevalue;

Session["MYXFEROBJECT"] = obj;
Server.Transfer("nextwebpage.aspx");
}
}

// Re-populate the page controls, handles "back" button and postbacks
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["MYXFEROBJECT"] != null)
RehydrateObj();
}
}

// Re-constitute the "pass-along" object and use the data fields required
for this page
private void RehydrateObj()
{
// cast the stored obj to MyXferObj type
MyXferObj obj = Session["MYXFEROBJECT"] ;

... Repopulate the page controls with the values from "obj"
}


My questions are:
1) I'm concerned about memory leaks - do I need to "release", set to null
the MyXferObj somewhere?
At each page, my Session["MYXFEROBJECT"] would be set to the "latest"
incarnation of a new MyXferObj - do I
need to free the "old" one somehow (if so, how?) ?

2) Is there a more preferred way to pass information between pages? I
prefer to have a single "data-struct" object passed if
possible, rather then creating an entry in the Session object for each
data item.
 
G

Guest

If you are using InProc Session state then you could clear it in the
Session_End event.
Yes, You could create something like a custom data Structure and store this
in the session state instead of storing each individual value.

If you are not using Dynamic compilation for pages, and using
Server.Transfer, you could use Context.handler (cast this to page1) and
access the DataStructure stored in Page1.
 
D

Dirc Khan-Evans

ESmith said:
I have multi-page form where I pass an object between pages as
follows:

On each page:

// What page to go to next
private void btnContinue_Click(object sender, System.EventArgs e)
{
if (Page.IsValid)
{
MyXferObj obj = new MyXferObj();
obj.dataitemX = somevalue;
obj.dataitemY = somevalue;

Session["MYXFEROBJECT"] = obj;
Server.Transfer("nextwebpage.aspx");
}
}

// Re-populate the page controls, handles "back" button and postbacks
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["MYXFEROBJECT"] != null)
RehydrateObj();
}
}

// Re-constitute the "pass-along" object and use the data fields
required for this page private void RehydrateObj()
{
// cast the stored obj to MyXferObj type
MyXferObj obj = Session["MYXFEROBJECT"] ;

... Repopulate the page controls with the values from "obj"
}


My questions are:
1) I'm concerned about memory leaks - do I need to "release", set to
null the MyXferObj somewhere? At each page, my
Session["MYXFEROBJECT"] would be set to the "latest" incarnation of a
new MyXferObj - do I need to free the "old" one somehow (if so,
how?) ?

I beleive this is cleared out for you when the session expires.. it is
essentially disposed.
2) Is there a more preferred way to pass information between pages?
I prefer to have a single "data-struct" object passed if possible,
rather then creating an entry in the Session object for each data
item.


Yeah.. a very common method is to create a struct for your data and
store that in the Session. you can then retrieve and update it as you
go along... Or use a hashtable which will let you store any number of
objects. This is the most extensible but the downside is that it is not
essentially strongly typed, so that it relies on you to know what you
have stored under what key.

====================================================================

There are all sorts of other ways to store data. If you wish to get a
bit of better performance you could disable Session state and instead
store things in the ViewState.. you can do this as long as the objects
you store are Serializable.

Or you could put it into some hidden field on your form (which is
essentially the old school way of doing things and this is what the
viewstate is doing anyway..)

It all depends on how much data you have, how often it changes and how
long you wish it to be available for.

Ahh.. decisions, decisions.

Dirc

--
 
K

Kevin Spencer

Okay, you're using Server.Transfer, so you're passing the entire HttpContext
when you pass the execution to the second page. Therefore, there is no need
to store anything in Session, or to do anything but expose the data via a
public field, method, or property in the first page. Example (for second
page code):

Page1ClassName firstPage = (Page1ClassName)Context.Handler;
// Get whatever you need from the first page now, as "firstPage"
1) I'm concerned about memory leaks - do I need to "release", set to null
the MyXferObj somewhere?

Setting a variable to null does nothing to release memory. As soon as the
thread which owns an object is removed from the stack (goes out of scope),
the object is available for Garbage Collection. If an object implements
IDisposable, it should be disposed. Otherwise, you can count on Garbage
Collection to manage memory for you.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

ESmith said:
I have multi-page form where I pass an object between pages as follows:

On each page:

// What page to go to next
private void btnContinue_Click(object sender, System.EventArgs e)
{
if (Page.IsValid)
{
MyXferObj obj = new MyXferObj();
obj.dataitemX = somevalue;
obj.dataitemY = somevalue;

Session["MYXFEROBJECT"] = obj;
Server.Transfer("nextwebpage.aspx");
}
}

// Re-populate the page controls, handles "back" button and postbacks
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["MYXFEROBJECT"] != null)
RehydrateObj();
}
}

// Re-constitute the "pass-along" object and use the data fields required
for this page
private void RehydrateObj()
{
// cast the stored obj to MyXferObj type
MyXferObj obj = Session["MYXFEROBJECT"] ;

... Repopulate the page controls with the values from "obj"
}


My questions are:
1) I'm concerned about memory leaks - do I need to "release", set to null
the MyXferObj somewhere?
At each page, my Session["MYXFEROBJECT"] would be set to the "latest"
incarnation of a new MyXferObj - do I
need to free the "old" one somehow (if so, how?) ?

2) Is there a more preferred way to pass information between pages? I
prefer to have a single "data-struct" object passed if
possible, rather then creating an entry in the Session object for each
data item.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top