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.