By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,165 Members | 1,928 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,165 IT Pros & Developers. It's quick & easy.

Problem passing custom object between forms

P: n/a
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");
}
}

Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Scott,

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

Jim Cheshire [MSFT]
Developer Support
ASP.NET
ja******@online.microsoft.com

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

--------------------
From: Scott Zabolotzky <za******@ripco.com>
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: <bn**********@e250.ripco.com>
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: us****@ripco.com
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!newsfeed 00.sul.t-online.de!t-onlin
e.de!newsfeed.icl.net!newsfeed.fjserv.net!newshost ing.com!news-xfer1.atl.new
shosting.com!diablo.voicenet.com!tdsnet-transit!newspeer.tds.net!gail.ripco.
com!zabolotsXref: 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");
}
}


Nov 17 '05 #2

P: n/a
ja******@online.microsoft.com (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
ja******@online.microsoft.com 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
Nov 17 '05 #3

P: n/a
Scott Zabolotzky <za******@ripco.com> writes:
ja******@online.microsoft.com (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
ja******@online.microsoft.com 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.

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

Nov 17 '05 #4

P: n/a
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
ja******@online.microsoft.com

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

--------------------
From: Scott Zabolotzky <za******@ripco.com>
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: <bn**********@e250.ripco.com>
References: <bn**********@e250.ripco.com> <DD**************@cpmsftngxa06.phx.gbl>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: us****@ripco.com
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!newsfeed 00.sul.t-online.de!t-onlin
e.de!diablo.theplanet.net!zen.net.uk!cox.net!news-xfer.cox.net!gail.ripco.co
m!zabolotsXref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:186125
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

ja******@online.microsoft.com (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
ja******@online.microsoft.com

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


Nov 17 '05 #5

P: n/a
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 [MSFT]" <ja******@online.microsoft.com> wrote in message
news:sR**************@cpmsftngxa06.phx.gbl...
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
ja******@online.microsoft.com

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

--------------------
From: Scott Zabolotzky <za******@ripco.com>
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: <bn**********@e250.ripco.com>
References: <bn**********@e250.ripco.com> <DD**************@cpmsftngxa06.phx.gbl>
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: us****@ripco.com
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!newsfeed 00.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:186125X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

ja******@online.microsoft.com (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
ja******@online.microsoft.com

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

Nov 17 '05 #6

P: n/a
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
ja******@online.microsoft.com

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

--------------------
From: "Jerry III" <je******@hotmail.com>
References: <bn**********@e250.ripco.com> <DD**************@cpmsftngxa06.phx.gbl> <bn**********@e250.ripco.com>
<sR**************@cpmsftngxa06.phx.gbl>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: <eP**************@TK2MSFTNGP10.phx.gbl>
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!TK2MSFTN GP10.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 [MSFT]" <ja******@online.microsoft.com> wrote in message
news:sR**************@cpmsftngxa06.phx.gbl...
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
ja******@online.microsoft.com

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

--------------------
>From: Scott Zabolotzky <za******@ripco.com>
>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: <bn**********@e250.ripco.com>
>References: <bn**********@e250.ripco.com>

<DD**************@cpmsftngxa06.phx.gbl>
>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: us****@ripco.com
>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!newsfee d00.sul.t-online.de!t-onli

n

e.de!diablo.theplanet.net!zen.net.uk!cox.net!ne ws-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
>
>ja******@online.microsoft.com (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
>>ja******@online.microsoft.com
>
>>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
>
>
>



Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.