Storing and Exposing a Complex Object Property In Viewstate

  • Thread starter Thread starter daokfella
  • Start date Start date
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?
 
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)
 
Back
Top