Storing and Exposing a Complex Object Property In Viewstate



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
object o = ViewState["Engine"];
if (o == null)
return new AutomobileEngine();
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

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?



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 (

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