Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / October 2005

Tip: Looking for answers? Try searching our database.

Help with transferring Session object between pages.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ESmith - 18 Oct 2005 21:23 GMT
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.
Aarthi - 18 Oct 2005 21:54 GMT
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.
Signature

Aarthi R S

> I have multi-page form where I pass an object between pages as follows:
>
[quoted text clipped - 45 lines]
>     possible, rather then creating an entry in the Session object for each
> data item.
Dirc Khan-Evans - 19 Oct 2005 09:21 GMT
> I have multi-page form where I pass an object between pages as
> follows:
[quoted text clipped - 40 lines]
> 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
Kevin Spencer - 19 Oct 2005 11:56 GMT
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.

Signature

HTH,

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

>I have multi-page form where I pass an object between pages as follows:
>
[quoted text clipped - 45 lines]
>    possible, rather then creating an entry in the Session object for each
> data item.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.