ViewState / Own Object

  • Thread starter Thread starter Rob
  • Start date Start date
R

Rob

Hi all,

I have my own object stored in the viewstate on one of my pages, as
the user progresses through the next couple of pages I want to update
it.

In order to get my object in there I had to make is serializable, my
question is now that its in there - how can I update it?

For example, I could take a copy of it from the viewstate, remove it
from the viewstate, update it, and then add it to the viewstate again
- seems a bit lengthly...so I was wondering, with it being in there -
am I able to just reference it directly or can I not do that once its
been serialized?

Any help would be appreciated.

Regards

Rob
 
myObject = (MyObject) ViewState["myObject"];
myObject.Property = newValue;
ViewState["myObject"] = myObject;

What's wrong with that?
 
THings that go into the ViewState (or Session or Application) are objects.

as in

dim o as Object
object o = null;

When you pull them out, you check that the key is not null/nothing , then
you have to CAST it as the correct object.

...

string empKey = "EmployeeKey";

Employee emp = new Employee();
emp.Name = "John Smith";

ViewState[empKey ] = emp;

then later:


string empKey = "EmployeeKey";
Employee foundEmployee = null;
if(null!=ViewState[empKey ])

foundEmployee = (Employee) ViewState[empKey ];

}

if(null!=foundEmployee)
{
foundEmployee.Name = "Mary Jones";
}


--------VB>NET


Dim empKey As String = "EmployeeKey"

Dim emp As New Employee()
emp.Name = "John Smith"

ViewState(empKey) = emp



Dim empKey As String = "EmployeeKey"
Dim foundEmployee As Employee = Nothing
If not (ViewState(empKey) is nothing) Then

foundEmployee = CType(ViewState(empKey), Employee)
End If


If not (foundEmployee is nothing ) Then
foundEmployee.Name = "Mary Jones"
End If
 
Thanks all....

I had got a bit confused with the serialization and wondered if I
would need to de-serialize before I could update the object and then
reserialize - a colleague put me straight! :o)

Regarding the session - nope - as that would time out, using the
viewstate as long as they dont close the browser all data entered thus
far will remain until they complete the operation.

Regards

Rob
 
Unless your "object" is very small, you will probably be better off using
Cache with a named unuque key (Such as Session.SessionId.ToString() +
"username" ) You can set the Cache priority to never remove, and so you won't
have the problem of session expiration.
Peter
 
Unless your "object" is very small, you will probably be better off using
Cache with a named unuque key (Such as Session.SessionId.ToString() +
"username" ) You can set the Cache priority to never remove, and so you
won't
have the problem of session expiration.

Hi Peter,

Thanks for the reply and info - I'm always open to new ideas, dont always
understand them at first though! :o)

The object is a UserRegistration object, is consists of about 30 variables
that hold strings, integers and a guid, also a couple of collection objects
which in turn hold strings/integers...

The object doesn't "feel" too big for the viewstate, and when I look at the
source, the text it generates is not particularly massive...

Any thoughts?

Rob
 
Back
Top