Storing and Exposing a Complex Object Property In Viewstate

D

daokfella

I have a custom server control that exposes a property which is a
complex type. I expose the property as such:

public class Automobile
{
public AutomobileEngine Engine
{
get
{
object o = ViewState["Engine"];
if (o == null)
return new AutomobileEngine();
else
return (AutomobileEngine)o;
}
private set
{
ViewState["Engine"] = value;
}
}
}

It seems that there is a problem persisting values of Engine in
ViewState when it's exposed in this manner. If I code something like
this...

MyAutomobile.Engine.Cylinders = 6;

....the value isn't persisted unless I re-assign the property....

AutomobileEngine eng = MyAutomobile.Engine;
eng.Cylinders = 123;
MyAutomobile.Engine. = eng;

Is there a better way of exposing the property so changing properties
of the complex object remain in ViewState without having to re-assign
it back into the ViewState?
 
B

bruce barker

you have a coding bug. if the object is not in view, you always return a new
object, you never store the new object in viewstate.


-- bruce (sqlwork.com)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top