Problem passing custom object between forms

Discussion in 'ASP .Net' started by Scott Zabolotzky, Oct 23, 2003.

  1. 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");
    }
    }
    Scott Zabolotzky, Oct 23, 2003
    #1
    1. Advertising

  2. Scott,

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

    Jim Cheshire [MSFT]
    Developer Support
    ASP.NET


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

    --------------------
    >From: Scott Zabolotzky <>
    >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: <bn8ub7$3ut$>
    >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:
    >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");
    > }
    >}
    >
    >
    >
    >
    Jim Cheshire [MSFT], Oct 23, 2003
    #2
    1. Advertising

  3. (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
    >


    >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
    Scott Zabolotzky, Oct 23, 2003
    #3
  4. Scott Zabolotzky <> writes:

    > (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
    >>


    >>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
    Scott Zabolotzky, Oct 23, 2003
    #4
  5. 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


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

    --------------------
    >From: Scott Zabolotzky <>
    >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: <bn9844$mp1$>
    >References: <bn8ub7$3ut$>

    <DD0y#>
    >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:
    >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
    >
    > (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
    >>

    >
    >>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
    >
    >
    >
    Jim Cheshire [MSFT], Oct 23, 2003
    #5
  6. Scott Zabolotzky

    Jerry III Guest

    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]" <> wrote in message
    news:...
    > 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
    >
    >
    > This post is provided as-is with no warranties and confers no rights.
    >
    > --------------------
    > >From: Scott Zabolotzky <>
    > >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: <bn9844$mp1$>
    > >References: <bn8ub7$3ut$>

    > <DD0y#>
    > >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:
    > >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
    > >
    > > (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
    > >>

    > >
    > >>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
    > >
    > >
    > >

    >
    Jerry III, Oct 24, 2003
    #6
  7. 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


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

    --------------------
    >From: "Jerry III" <>
    >References: <bn8ub7$3ut$>

    <DD0y#> <bn9844$mp1$>
    <>
    >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: <>
    >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 [MSFT]" <> wrote in message
    >news:...
    >> 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
    >>
    >>
    >> This post is provided as-is with no warranties and confers no rights.
    >>
    >> --------------------
    >> >From: Scott Zabolotzky <>
    >> >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: <bn9844$mp1$>
    >> >References: <bn8ub7$3ut$>

    >> <DD0y#>
    >> >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:
    >> >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

    n
    >>

    >e.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
    >> >
    >> > (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
    >> >>
    >> >
    >> >>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
    >> >
    >> >
    >> >

    >>

    >
    >
    >
    Jim Cheshire [MSFT], Oct 24, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mathew George

    Passing server controls between forms

    Mathew George, Jul 7, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    939
    Mathew George
    Jul 7, 2003
  2. terry
    Replies:
    1
    Views:
    459
    William F. Robertson, Jr.
    Jul 7, 2003
  3. cgia
    Replies:
    5
    Views:
    756
    Steve C. Orr, MCSD
    Jul 26, 2003
  4. =?Utf-8?B?bXN5ZXo=?=

    passing data between forms in the "cleanest" OO way?

    =?Utf-8?B?bXN5ZXo=?=, Feb 5, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    375
    Felbrigg
    Feb 5, 2004
  5. Eric
    Replies:
    2
    Views:
    467
Loading...

Share This Page