Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Passing objects forth and back between ASP.NET pages.

Passing objects forth and back between ASP.NET pages.

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netsysadminhelptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MY1201
    wrote on last edited by
    #1

    Hi there. I'm currently creating a site which behaves like a kind of "wizard". So I need to pass objects forth and back between the pages in the wizard. Now this works fine passing objects forth, but I have a problem passing objects back or have the page remembering objects between post backs. Here's some example code:

    private MyObject \_myObject;
    
    protected void Page\_Load(object sender, EventArgs e)
    {
        // Context.Items\["MyObject"\] has been set by another page. 
        // An passed on using Server.Transfer()
        \_myObject = (MyObject)Context.Items\["MyObject"\]; 
    
        \_myObject.MyString = "Hello";
    }
    
    protected void Button1\_Click(object sender, EventArgs e)
    {
        MyObjects.SaveMyObject(\_myObject);
    }
    

    Here's my problem: On my web form I have a button which calls "Button1_Click". Whenever this method is called I get a "NullReferenceException" from SaveMyObject - obviously because _myObject is null. But why? In the Page_Load method setting the MyString property to "Hello" works just fine. So my guess is that the page "forgets" the content of the context object between post backs. I guess I need to do something else. Is there some kind of solution to this problem? Best regards Soeren

    M L 2 Replies Last reply
    0
    • M MY1201

      Hi there. I'm currently creating a site which behaves like a kind of "wizard". So I need to pass objects forth and back between the pages in the wizard. Now this works fine passing objects forth, but I have a problem passing objects back or have the page remembering objects between post backs. Here's some example code:

      private MyObject \_myObject;
      
      protected void Page\_Load(object sender, EventArgs e)
      {
          // Context.Items\["MyObject"\] has been set by another page. 
          // An passed on using Server.Transfer()
          \_myObject = (MyObject)Context.Items\["MyObject"\]; 
      
          \_myObject.MyString = "Hello";
      }
      
      protected void Button1\_Click(object sender, EventArgs e)
      {
          MyObjects.SaveMyObject(\_myObject);
      }
      

      Here's my problem: On my web form I have a button which calls "Button1_Click". Whenever this method is called I get a "NullReferenceException" from SaveMyObject - obviously because _myObject is null. But why? In the Page_Load method setting the MyString property to "Hello" works just fine. So my guess is that the page "forgets" the content of the context object between post backs. I guess I need to do something else. Is there some kind of solution to this problem? Best regards Soeren

      M Offline
      M Offline
      M LN Rao
      wrote on last edited by
      #2

      Try to place the Page_Load code in Button Click, perhaps it may solve your problem

      1 Reply Last reply
      0
      • M MY1201

        Hi there. I'm currently creating a site which behaves like a kind of "wizard". So I need to pass objects forth and back between the pages in the wizard. Now this works fine passing objects forth, but I have a problem passing objects back or have the page remembering objects between post backs. Here's some example code:

        private MyObject \_myObject;
        
        protected void Page\_Load(object sender, EventArgs e)
        {
            // Context.Items\["MyObject"\] has been set by another page. 
            // An passed on using Server.Transfer()
            \_myObject = (MyObject)Context.Items\["MyObject"\]; 
        
            \_myObject.MyString = "Hello";
        }
        
        protected void Button1\_Click(object sender, EventArgs e)
        {
            MyObjects.SaveMyObject(\_myObject);
        }
        

        Here's my problem: On my web form I have a button which calls "Button1_Click". Whenever this method is called I get a "NullReferenceException" from SaveMyObject - obviously because _myObject is null. But why? In the Page_Load method setting the MyString property to "Hello" works just fine. So my guess is that the page "forgets" the content of the context object between post backs. I guess I need to do something else. Is there some kind of solution to this problem? Best regards Soeren

        L Offline
        L Offline
        l0kke
        wrote on last edited by
        #3

        AFAIK, Context.Items is used to store values within one request. But you wrote, you can pass values between two pages, so I don't know, maybe I'm mistaken. Anyway, if you use Session object instead of Context.Items, it should works fine. Pilo

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups