Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Storing and Exposing a Complex Object Property In Viewstate

Reply
Thread Tools

Storing and Exposing a Complex Object Property In Viewstate

 
 
daokfella
Guest
Posts: n/a
 
      08-08-2008
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?
 
Reply With Quote
 
 
 
 
bruce barker
Guest
Posts: n/a
 
      08-08-2008
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?
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
storing pointer vs storing object toton C++ 11 10-13-2006 11:08 AM
Exposing an event handler as a control property Paul ASP .Net 1 08-28-2006 05:33 AM
WebService returns object - storing that object in ViewState Mark ASP .Net 3 07-18-2006 02:12 PM
exposing NameValueCollection Property through ASPX page ? (string representation of its values) sonic ASP .Net 1 01-07-2005 06:15 PM
Exposing Font Name Property for Custom Control Deep S. ASP .Net Building Controls 2 08-04-2003 10:17 AM



Advertisments