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. ViewState storing custom class

ViewState storing custom class

Scheduled Pinned Locked Moved ASP.NET
question
11 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.
  • F Fayu

    How do I store a custom object in a ViewState. I know that viewstates do not support XmlSerialization. I currently have a class that is using XmlSerialization and all hell is breaking lose when trying to set a viewstate entry to that value.

    T Offline
    T Offline
    ToddHileHoffer
    wrote on last edited by
    #2

    ViewState is actually a name / value collection. You can't store a class in ViewState. You can store a serializable object in Session though.

    I didn't get any requirements for the signature

    F 1 Reply Last reply
    0
    • T ToddHileHoffer

      ViewState is actually a name / value collection. You can't store a class in ViewState. You can store a serializable object in Session though.

      I didn't get any requirements for the signature

      F Offline
      F Offline
      Fayu
      wrote on last edited by
      #3

      You can store an object inside a viewstate as long as it is BinaryFormat-able.

      T 1 Reply Last reply
      0
      • F Fayu

        You can store an object inside a viewstate as long as it is BinaryFormat-able.

        T Offline
        T Offline
        ToddHileHoffer
        wrote on last edited by
        #4

        So is that what you are doing? In my 8 years of doing asp.net I have never once tried to store an object in viewstate. Can ask why you want to store an object in viewstate?

        I didn't get any requirements for the signature

        F 1 Reply Last reply
        0
        • T ToddHileHoffer

          So is that what you are doing? In my 8 years of doing asp.net I have never once tried to store an object in viewstate. Can ask why you want to store an object in viewstate?

          I didn't get any requirements for the signature

          F Offline
          F Offline
          Fayu
          wrote on last edited by
          #5

          I am trying to store the object in a control. What do you suggest?

          T 1 Reply Last reply
          0
          • F Fayu

            I am trying to store the object in a control. What do you suggest?

            T Offline
            T Offline
            ToddHileHoffer
            wrote on last edited by
            #6

            Well, if you have a base page class or a master page. Store the object in session. The reason for the base page class or master page is that you can check what aspx page is being loaded and set the session object to null if it is no longer needed. So it will behave like viewstate but you won't have all that data traveling back and forth to the client.

            I didn't get any requirements for the signature

            D F 2 Replies Last reply
            0
            • T ToddHileHoffer

              Well, if you have a base page class or a master page. Store the object in session. The reason for the base page class or master page is that you can check what aspx page is being loaded and set the session object to null if it is no longer needed. So it will behave like viewstate but you won't have all that data traveling back and forth to the client.

              I didn't get any requirements for the signature

              D Offline
              D Offline
              DoctorMick
              wrote on last edited by
              #7

              In the absence of a master page or base class you can also use the global.asax to manage the session objects.

              T 1 Reply Last reply
              0
              • D DoctorMick

                In the absence of a master page or base class you can also use the global.asax to manage the session objects.

                T Offline
                T Offline
                ToddHileHoffer
                wrote on last edited by
                #8

                Thanks for the tip, I never thought of that. Does the Application_BeginRequest event fire on every post back?

                I didn't get any requirements for the signature

                D 1 Reply Last reply
                0
                • T ToddHileHoffer

                  Thanks for the tip, I never thought of that. Does the Application_BeginRequest event fire on every post back?

                  I didn't get any requirements for the signature

                  D Offline
                  D Offline
                  DoctorMick
                  wrote on last edited by
                  #9

                  I think so but the way I usually do it is to wire up an event to page PreInit in Application_PreRequestHandlerExecute. It might not be the best way to do it (so I'm open to suggestions) but it works a treat.

                  1 Reply Last reply
                  0
                  • T ToddHileHoffer

                    Well, if you have a base page class or a master page. Store the object in session. The reason for the base page class or master page is that you can check what aspx page is being loaded and set the session object to null if it is no longer needed. So it will behave like viewstate but you won't have all that data traveling back and forth to the client.

                    I didn't get any requirements for the signature

                    F Offline
                    F Offline
                    Fayu
                    wrote on last edited by
                    #10

                    I wanted to avoid using session vars, but I guess that is my only option at the moment. Thanks!

                    T 1 Reply Last reply
                    0
                    • F Fayu

                      I wanted to avoid using session vars, but I guess that is my only option at the moment. Thanks!

                      T Offline
                      T Offline
                      ToddHileHoffer
                      wrote on last edited by
                      #11

                      Just one more tip for you. Create a static class to hold all your session objects. For example

                      public static class SessionItems
                      {
                      public static YourObjectType YourObject
                      {
                      get
                      {
                      Object result = HttpContext.Current.Session["YourObject"];
                      if (result == null)
                      return null;
                      else
                      return (YourObjectType)result;
                      }
                      set { HttpContext.Current.Session["YourObject"] = value; }
                      }
                      }

                      It makes it easier to manager your session objects this way, plus it prevents bugs from trying to access a session object that does not exist. Also prevents bugs that can occur from mispellings

                      I didn't get any requirements for the signature

                      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