Storing and Exposing a Complex Object Property In Viewstate

Discussion in 'ASP .Net' started by daokfella, Aug 8, 2008.

  1. daokfella

    daokfella Guest

    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?
     
    daokfella, Aug 8, 2008
    #1
    1. Advertising

  2. daokfella

    bruce barker Guest

    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 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?
    >
     
    bruce barker, Aug 8, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sonic
    Replies:
    1
    Views:
    1,967
    John Saunders
    Jan 7, 2005
  2. Mark
    Replies:
    3
    Views:
    4,039
    Steven Cheng[MSFT]
    Jul 18, 2006
  3. Paul
    Replies:
    1
    Views:
    439
  4. toton
    Replies:
    11
    Views:
    742
    toton
    Oct 13, 2006
  5. Deep S.

    Exposing Font Name Property for Custom Control

    Deep S., Aug 4, 2003, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    195
    Deep S.
    Aug 4, 2003
Loading...

Share This Page