Storing and Exposing a Complex Object Property In Viewstate

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • daokfella

    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 AutomobileEngin e Engine
    {
    get
    {
    object o = ViewState["Engine"];
    if (o == null)
    return new AutomobileEngin e();
    else
    return (AutomobileEngi ne)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.En gine.Cylinders = 6;

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

    AutomobileEngin e eng = MyAutomobile.En gine;
    eng.Cylinders = 123;
    MyAutomobile.En gine. = 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?
  • =?Utf-8?B?YnJ1Y2UgYmFya2Vy?=

    #2
    RE: Storing and Exposing a Complex Object Property In 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)


    "daokfella" wrote:
    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 AutomobileEngin e Engine
    {
    get
    {
    object o = ViewState["Engine"];
    if (o == null)
    return new AutomobileEngin e();
    else
    return (AutomobileEngi ne)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.En gine.Cylinders = 6;
    >
    ....the value isn't persisted unless I re-assign the property....
    >
    AutomobileEngin e eng = MyAutomobile.En gine;
    eng.Cylinders = 123;
    MyAutomobile.En gine. = 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?
    >

    Comment

    Working...