Link to home
Start Free TrialLog in
Avatar of Siberwulf
Siberwulf

asked on

Custom object in either Viewstate or session state

I want to have an object that is in the session state or view state of a page.  I want it to retain all the data it acquires via postsbacks.  Is there anyway to do this?  Do I need to serialize it to put it in a viewstate?  I really need some help here...

This is what I have so far:

<SNIP>
   Public Property SessionObject() As testclass
       Get
           Return Session("Var")
       End Get
       Set(ByVal objObject As testclass)
           Session.Add(("Var"), objObject)
       End Set
   End Property

   Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       If Not IsPostBack Then
           SessionObject = New testclass(lblTest)
       End If
   End Sub

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       SessionObject.StrText = "Updated"
       SessionObject.Update()
   End Sub
</SNIP>

Inside my testclass.vb:

<SNIP>
Public Class testclass

   Dim lblLabel As Web.UI.WebControls.Label
   Dim m_Text As String

   Public Sub New(ByRef ParamLabel As Web.UI.WebControls.Label)
       lblLabel = ParamLabel
   End Sub

   Public Property StrText() As String
       Get
           Return m_Text
       End Get
       Set(ByVal strText As String)
           m_Text = strText
       End Set
   End Property

   Public Sub Update()
       lblLabel.Text = m_Text
   End Sub

End Class
</SNIP>
Avatar of tockhoi
tockhoi

You just forgot to cast.

    Public Property SessionObject() As testclass
        Get
            Return CType(Session("Var"), testclass)
        End Get
        Set(ByVal Value As testclass)
            Session("Var") = Value
        End Set
    End Property

I think that should work now.
Avatar of Siberwulf

ASKER

That actually didn't help the problem at all :(

The form is submitting, and posting back, but the information that was stored in the line:

 SessionObject.StrText = "Updated

is never saved for some reason
See if this helps...

Change the public property SessionObject() to:

  Public Property SessionObject() As testclass
      Get
          Return Session("Var")
      End Get
      Set(ByVal objObject As testclass)
          Session("Var") = objObject
      End Set
  End Property

Now, Session("Var")  has your object.

Hope this helps....
How did you test it didn't save? When I tried putting a Response.Write in the Button click it showed "updated"

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      SessionObject.StrText = "Updated"
      SessionObject.Update()
      Response.Write (SessionObject.StrText)  ' I guess it's obvious it would print here
  End Sub
Try testing it like this :-

Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    'SessionObject.StrText = "Updated"
    'SessionObject.Update()
    Dim tc As TestClass = CType(SessionObject,TestClass)
    Response.Write("Input Is " & tc.StrText)            
    tc.StrText ="Updated"
    tc.Update()
    SessionObject = tc
End Sub

The first click displays Input Is
Subsequent clicks display Input Is Updated
SOLUTION
Avatar of amit_g
amit_g
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Isn't that the same thing thats being done in my constructor:


    Public Sub New(ByRef ParamLabel As Web.UI.WebControls.Label)
        lblLabel = ParamLabel
    End Sub

?

I want to load the object up one time, when the users FIRST goes to the page.  AFter that, I want to pull from the session-type variable and use the data that was there before they posted-back.

And amit_g:

I didn't want them to manage themselves becuase there would be too much non-flexible, redundant code.  The project I'm doing is like a checklist, with 3 radio buttons per question.  I want to be able to update the database based on each radio button click and postback.  Additionally, i want to be able to disable question on the fly, without 3 lines of blahblah.disable when i could do a Question1.Disable() (as a method I created in my classs)  I can post more pertinent code if need be.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No. It fifferent. Just add the code in your existing code (no need to change a single line of your own code other than this)

The controls have to be created in the page (in my code the lbltest in the page, .aspx)
It pass this control to the class (testclass.vb), then both of them are refer to the same control.

So, for every control you have to provide a simalar property as the objLabel in my code to pass the reference.

'your code is only good without the page.ispostback check portion of your code.
Public Sub New(ByRef ParamLabel As Web.UI.WebControls.Label)
       lblLabel = ParamLabel
   End Sub
'to see exactly,  comment out the check Ispostback code, when you click the button, "update" will show in the label
 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      'If Not IsPostBack Then
          SessionObject = New testclass(lblTest)
      'End If
  End Sub


'once you check postback, the lbltest you passed to your testclass constructor is not the new lbltest created in the page.
you will need my code to pass the new created reference to the testclass.
I finally got it to work though, by writing a separate function to 're-initialize' each object on the page (for my custom app, this was just a scaled down version)