Problem passing custom object between forms

S

Scott Zabolotzky

I'm trying to pass a custom object back and forth between forms.
This custom object is pulled into the app using an external reference
to an assembly DLL that was given to me by a co-worker. A query-string
flag is used to indicate to the page whether it should instantiate
a new instance of the object or access an existing instance from
the calling page.

On the both pages I have a property of the page which is an
instance of this custom object. I also have a public get accessor
in the page. When a button is clicked I use Server.Transfer to
jump to the other page.

When each page is loaded I look for a query-string to determine
if this page was loaded by the user directly (query-string not
found) or if it was the result of a transfer from the other page
(query-string found). I create an instance of the sending page using
the Context.Handler and assign a local variable of my custom
object type using the get accessor of the sending page.

Unfortunately this isn't working. When I try to access the
custom object I just get "Undefined Value". I can transfer string data
from one form to the next just fine but the custom object does not
work.

Any ideas? Here's my code.

public class WebForm1: System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button send;
protected MyNamespace.MyObject obj;
protected string test;

public MyNamespace.MyObject Object
{
get
{
return obj;
}
}

public string Test
{
get
{
return test;
}
}

private void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{
string sTransfer = Request.QueryString["transfer"];
if (sTransfer == "true")
{
//We were transferred here from WebForm2
//Extract WebForm2's object
WebForm2 wf2;
wf2 = (WebForm2)Context.Handler;
obj = wf2.Object;

//extract WebForm2's string
test = wf2.Test;
}
else
{
//this is the initial page load,
//not a transfer from WebForm2
obj = new MyNamespace.MyObject("data");

//Assign test string a value
test = "test assigned by WebForm1";
}
}

}

private void send_Click(object sender, System.EventArgs e)
{
Server.Transfer ("WebForm2.aspx?transfer=true");
}
}

public class WebForm2: System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button send;
protected MyNamespace.MyObject obj;
protected string test;

public MyNamespace.MyObject Object
{
get
{
return obj;
}
}

public string Test
{
get
{
return test;
}
}

private void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{
string sTransfer = Request.QueryString["transfer"];
if (sTransfer == "true")
{
//We were transferred here from WebForm1
//Extract WebForm1's object
WebForm1 wf1;
wf1 = (WebForm1)Context.Handler;
obj = wf1.Object;

//extract WebForm2's string
test = wf1.Test;
}
else
{
//this is the initial page load,
//not a transfer from WebForm1
obj = new MyNamespace.MyObject("data");

//Assign test string a value
test = "test assigned by WebForm2";
}
}

}

private void send_Click(object sender, System.EventArgs e)
{
Server.Transfer ("WebForm1.aspx?transfer=true");
}
}
 
J

Jim Cheshire [MSFT]

Scott,

It's much easier just to pass your object in a Session variable.

Jim Cheshire [MSFT]
Developer Support
ASP.NET
(e-mail address removed)

This post is provided as-is with no warranties and confers no rights.

--------------------
From: Scott Zabolotzky <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet
Subject: Problem passing custom object between forms
Date: Thu, 23 Oct 2003 16:07:03 +0000 (UTC)
Organization: Ripco Communications Inc.
Lines: 142
Message-ID: <[email protected]>
NNTP-Posting-Host: shell2.ripco.com
X-Trace: e250.ripco.com 1066925223 4061 209.100.225.144 (23 Oct 2003 16:07:03 GMT)
X-Complaints-To: (e-mail address removed)
NNTP-Posting-Date: Thu, 23 Oct 2003 16:07:03 +0000 (UTC)
User-Agent: nn/6.6.5
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!newsfeed.icl.net!newsfeed.fjserv.net!newshosting.com!news-xfer1.atl.new
shosting.com!diablo.voicenet.com!tdsnet-transit!newspeer.tds.net!gail.ripco.
com!zabolots
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:186088
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

I'm trying to pass a custom object back and forth between forms.
This custom object is pulled into the app using an external reference
to an assembly DLL that was given to me by a co-worker. A query-string
flag is used to indicate to the page whether it should instantiate
a new instance of the object or access an existing instance from
the calling page.

On the both pages I have a property of the page which is an
instance of this custom object. I also have a public get accessor
in the page. When a button is clicked I use Server.Transfer to
jump to the other page.

When each page is loaded I look for a query-string to determine
if this page was loaded by the user directly (query-string not
found) or if it was the result of a transfer from the other page
(query-string found). I create an instance of the sending page using
the Context.Handler and assign a local variable of my custom
object type using the get accessor of the sending page.

Unfortunately this isn't working. When I try to access the
custom object I just get "Undefined Value". I can transfer string data
from one form to the next just fine but the custom object does not
work.

Any ideas? Here's my code.

public class WebForm1: System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button send;
protected MyNamespace.MyObject obj;
protected string test;

public MyNamespace.MyObject Object
{
get
{
return obj;
}
}

public string Test
{
get
{
return test;
}
}

private void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{
string sTransfer = Request.QueryString["transfer"];
if (sTransfer == "true")
{
//We were transferred here from WebForm2
//Extract WebForm2's object
WebForm2 wf2;
wf2 = (WebForm2)Context.Handler;
obj = wf2.Object;

//extract WebForm2's string
test = wf2.Test;
}
else
{
//this is the initial page load,
//not a transfer from WebForm2
obj = new MyNamespace.MyObject("data");

//Assign test string a value
test = "test assigned by WebForm1";
}
}

}

private void send_Click(object sender, System.EventArgs e)
{
Server.Transfer ("WebForm2.aspx?transfer=true");
}
}

public class WebForm2: System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button send;
protected MyNamespace.MyObject obj;
protected string test;

public MyNamespace.MyObject Object
{
get
{
return obj;
}
}

public string Test
{
get
{
return test;
}
}

private void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{
string sTransfer = Request.QueryString["transfer"];
if (sTransfer == "true")
{
//We were transferred here from WebForm1
//Extract WebForm1's object
WebForm1 wf1;
wf1 = (WebForm1)Context.Handler;
obj = wf1.Object;

//extract WebForm2's string
test = wf1.Test;
}
else
{
//this is the initial page load,
//not a transfer from WebForm1
obj = new MyNamespace.MyObject("data");

//Assign test string a value
test = "test assigned by WebForm2";
}
}

}

private void send_Click(object sender, System.EventArgs e)
{
Server.Transfer ("WebForm1.aspx?transfer=true");
}
}
 
S

Scott Zabolotzky

It's much easier just to pass your object in a Session variable.
Jim Cheshire [MSFT]
Developer Support
ASP.NET
(e-mail address removed)
This post is provided as-is with no warranties and confers no rights.


Jim,

Thanks for the suggestion. However I have a few ?'s:

1) I thought that passing data in a Session variable was to be
avoided due to memory constraints on the server. Is this not true?

2) Is there anything obvious from my code that is preventing my
custom object from being passed between forms using the Context.Handler
method? It works just fine for passing the string back and forth.

Thanks...Scott
 
S

Scott Zabolotzky

Scott Zabolotzky said:
(e-mail address removed) (Jim Cheshire [MSFT]) writes:
Scott,
It's much easier just to pass your object in a Session variable.
Jim Cheshire [MSFT]
Developer Support
ASP.NET
(e-mail address removed)
This post is provided as-is with no warranties and confers no rights.

Thanks for the suggestion. However I have a few ?'s:
1) I thought that passing data in a Session variable was to be
avoided due to memory constraints on the server. Is this not true?
2) Is there anything obvious from my code that is preventing my
custom object from being passed between forms using the Context.Handler
method? It works just fine for passing the string back and forth.


I tried using a session variable and I'm still getting
"undefined value"

Here's what I added:

In WebForm1 send_ButtonClicked:

Session["data"] = (object)obj;

In WebForm2 Page_Load

obj = (MyNamespace.MyObject)Session["data"]

Does the MyNamespace.MyObject type need to derive off of System.Object
for these methods of transferring data between forms to work? Could
that be why I'm getting "undefined value" errors?

Scott
 
J

Jim Cheshire [MSFT]

Scott,

Anyone that tells you that you shouldn't store objects in Session variables
is just plain wrong. Remember, in .NET, EVERYTHING is an object.
Therefore, if you are to say "don't store objects in a Session variable",
you are saying "don't use Session variables."

With that as a qualifier, it is true that you should evaluate using
Sessions to store objects based upon the size of the object.

As far as using your method of accessing the object via the Context, that
should work fine. I tried to reproduce your problem, and I am not able to.
It works fine for me. If you want me to test your project, please zip up
ALL project files and e-mail it to me at the e-mail address in my sig line,
but leave off the ONLINE part.

Thanks.

Jim Cheshire [MSFT]
Developer Support
ASP.NET
(e-mail address removed)

This post is provided as-is with no warranties and confers no rights.

--------------------
From: Scott Zabolotzky <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet
Subject: Re: Problem passing custom object between forms
Date: Thu, 23 Oct 2003 18:53:56 +0000 (UTC)
Organization: Ripco Communications Inc.
Lines: 28
Message-ID: <[email protected]>
References: <[email protected]>
NNTP-Posting-Host: shell2.ripco.com
X-Trace: e250.ripco.com 1066935236 23329 209.100.225.144 (23 Oct 2003 18:53:56 GMT)
X-Complaints-To: (e-mail address removed)
NNTP-Posting-Date: Thu, 23 Oct 2003 18:53:56 +0000 (UTC)
User-Agent: nn/6.6.5
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!diablo.theplanet.net!zen.net.uk!cox.net!news-xfer.cox.net!gail.ripco.co
m!zabolots
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:186125
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

[email protected] (Jim Cheshire [MSFT]) said:
It's much easier just to pass your object in a Session variable.
Jim Cheshire [MSFT]
Developer Support
ASP.NET
(e-mail address removed)
This post is provided as-is with no warranties and confers no rights.


Jim,

Thanks for the suggestion. However I have a few ?'s:

1) I thought that passing data in a Session variable was to be
avoided due to memory constraints on the server. Is this not true?

2) Is there anything obvious from my code that is preventing my
custom object from being passed between forms using the Context.Handler
method? It works just fine for passing the string back and forth.

Thanks...Scott
 
J

Jerry III

Anyone who tells you not to store objects in session variables has done a
little more coding than a simple Hello World in VB.Net. Sessions break down
too easily to store objects in them (try opening two browser windows after
you create the session and alternate requests between those windows). The
exception is read only objects, such as user preferences.

Jerry

Jim Cheshire said:
Scott,

Anyone that tells you that you shouldn't store objects in Session variables
is just plain wrong. Remember, in .NET, EVERYTHING is an object.
Therefore, if you are to say "don't store objects in a Session variable",
you are saying "don't use Session variables."

With that as a qualifier, it is true that you should evaluate using
Sessions to store objects based upon the size of the object.

As far as using your method of accessing the object via the Context, that
should work fine. I tried to reproduce your problem, and I am not able to.
It works fine for me. If you want me to test your project, please zip up
ALL project files and e-mail it to me at the e-mail address in my sig line,
but leave off the ONLINE part.

Thanks.

Jim Cheshire [MSFT]
Developer Support
ASP.NET
(e-mail address removed)

This post is provided as-is with no warranties and confers no rights.

--------------------
From: Scott Zabolotzky <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet
Subject: Re: Problem passing custom object between forms
Date: Thu, 23 Oct 2003 18:53:56 +0000 (UTC)
Organization: Ripco Communications Inc.
Lines: 28
Message-ID: <[email protected]>
References: <[email protected]>
NNTP-Posting-Host: shell2.ripco.com
X-Trace: e250.ripco.com 1066935236 23329 209.100.225.144 (23 Oct 2003 18:53:56 GMT)
X-Complaints-To: (e-mail address removed)
NNTP-Posting-Date: Thu, 23 Oct 2003 18:53:56 +0000 (UTC)
User-Agent: nn/6.6.5
Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-online.de!diablo.theplanet.net!zen.net.uk!cox.net!news-xfer.cox.net!gail.ripco.co
m!zabolots
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:186125
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

[email protected] (Jim Cheshire [MSFT]) said:
It's much easier just to pass your object in a Session variable.
Jim Cheshire [MSFT]
Developer Support
ASP.NET
(e-mail address removed)
This post is provided as-is with no warranties and confers no rights.


Jim,

Thanks for the suggestion. However I have a few ?'s:

1) I thought that passing data in a Session variable was to be
avoided due to memory constraints on the server. Is this not true?

2) Is there anything obvious from my code that is preventing my
custom object from being passed between forms using the Context.Handler
method? It works just fine for passing the string back and forth.

Thanks...Scott
 
J

Jim Cheshire [MSFT]

Jerry,

In .NET development, everything is an object. If you store a String in a
session variable, you are storing an object in a Session. Therefore, to
say "you should not store objects in Session variables" is wrong on its
face. The .NET Framework was designed to store objects in Session state.
In fact, the developers explicitly designed it with the ability to pass
simple objects and their properties in Session variables.

When you are writing code, nothing is absolute. If you have certain
architectures that make using Session variables problematic, it is your
responsibilty as the developer to identify those and make the necessary
accommodations.

Jim Cheshire [MSFT]
Developer Support
ASP.NET
(e-mail address removed)

This post is provided as-is with no warranties and confers no rights.

--------------------
From: "Jerry III" <[email protected]>
References: <[email protected]>
Subject: Re: Problem passing custom object between forms
Date: Thu, 23 Oct 2003 21:49:24 -0700
Lines: 96
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet
NNTP-Posting-Host: h-68-164-57-117.lsanca54.dynamic.covad.net 68.164.57.117
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:186243
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

Anyone who tells you not to store objects in session variables has done a
little more coding than a simple Hello World in VB.Net. Sessions break down
too easily to store objects in them (try opening two browser windows after
you create the session and alternate requests between those windows). The
exception is read only objects, such as user preferences.

Jerry

Jim Cheshire said:
Scott,

Anyone that tells you that you shouldn't store objects in Session variables
is just plain wrong. Remember, in .NET, EVERYTHING is an object.
Therefore, if you are to say "don't store objects in a Session variable",
you are saying "don't use Session variables."

With that as a qualifier, it is true that you should evaluate using
Sessions to store objects based upon the size of the object.

As far as using your method of accessing the object via the Context, that
should work fine. I tried to reproduce your problem, and I am not able to.
It works fine for me. If you want me to test your project, please zip up
ALL project files and e-mail it to me at the e-mail address in my sig line,
but leave off the ONLINE part.

Thanks.

Jim Cheshire [MSFT]
Developer Support
ASP.NET
(e-mail address removed)

This post is provided as-is with no warranties and confers no rights.

--------------------
From: Scott Zabolotzky <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.aspnet
Subject: Re: Problem passing custom object between forms
Date: Thu, 23 Oct 2003 18:53:56 +0000 (UTC)
Organization: Ripco Communications Inc.
Lines: 28
Message-ID: <[email protected]>
References: <[email protected]>
NNTP-Posting-Host: shell2.ripco.com
X-Trace: e250.ripco.com 1066935236 23329 209.100.225.144 (23 Oct 2003 18:53:56 GMT)
X-Complaints-To: (e-mail address removed)
NNTP-Posting-Date: Thu, 23 Oct 2003 18:53:56 +0000 (UTC)
User-Agent: nn/6.6.5
Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onli ne.de!diablo.theplanet.net!zen.net.uk!cox.net!news-xfer.cox.net!gail.ripco.c o
m!zabolots
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:186125
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

(e-mail address removed) (Jim Cheshire [MSFT]) writes:

Scott,

It's much easier just to pass your object in a Session variable.

Jim Cheshire [MSFT]
Developer Support
ASP.NET
(e-mail address removed)

This post is provided as-is with no warranties and confers no rights.


Jim,

Thanks for the suggestion. However I have a few ?'s:

1) I thought that passing data in a Session variable was to be
avoided due to memory constraints on the server. Is this not true?

2) Is there anything obvious from my code that is preventing my
custom object from being passed between forms using the Context.Handler
method? It works just fine for passing the string back and forth.

Thanks...Scott
 

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

Top