I'm afraid you've lost me a bit with what you have said (doesn't quite make
sense ...to me at least)
I completely understand using the state management techiques (so not a
problem there). Calling methods on the object saved in view state via
property is effectively making a copy of that serialized instance to memory
so that if i did the following for a collection object stored in viewstate
and accessed via property BigCollection....
BigCollection.MoveToRecord(10);
will only perform that operation on the "instance" returned/ deserialized
via the property from viewstate.
It will not perform that operation on the serialized instance stored in
viewstate. I would explicitly have to make sure that I overwrote that item
thus...
ACollection col;
col = BigCollection;
col.MoveToRecord(10);
BigCollection = col;
As you say, I know that I don't *have* to reassign it to ViewState if I
don't want changes to be written back, but that is exactly what I need.
My suggestion in my original post is simply that Web Controls perform their
own state management and that makes things easy (the whole reason MS
introduced them), I just thought that it should be possible to "mark"
variables valuetypes and reftype objects (that can be serialized) to force
asp.net to persist them -if we wanted.
This would surely make state management so much easier to do, less code to
write and less scope for bugs.
Thanks,
Mark.
William F. Robertson said:
The code snippet by far does NOT manage the state of myPerson. The only
thing explicit about it, is I am explicitly storing the item in ViewState.
I could have placed it in Session, Application, Cache, or could have
implemented explicit state management.
ViewState["Person"] points to an instance of myPerson.
Person points to the same instance.
Because ViewState["Person"] points to an instance, no matter how complex
your methods calls are on it, your Person is not actually saved to
ViewState
until PreRender. You can make all the changes you want to the internal
workings of your Person object, without reassigning it to ViewState.
How would you expected to have this item persisted?
bill
Mark Broadbent said:
You are having to explicitly implement coding to manage the state of this
variable and we are talking more than a couple of lines -especially
when
the
instance variable in question is quite complex such as when it has method
calls which change its state (that must be saved).
Thanks for suggestion though - it is probably the cleanest way to do this
(as I unfortunately thought).
Br,
Mark.
This is a perfect candidate for a property.
I type:
protected Person myPerson;
Then I run a macro that turns the above code into:
protected Person myPerson
{
get {
Person p = ViewState["Person"] as Person;
if ( p == null ) {
p = new Person( "Mr Happy I don't have to type this every
time." );
ViewState["Person"] = value;
}
return p;
}
set { ViewState["Person"] = value; }
}
In code I only reference myPerson. You can also continue to set it in
Page.Load !PostBack and leave out the new declaration in the property.
Been a while since I've touched asp.net but one thing that always
seems
to
fustrate me is the loss of state on variable declarations. Is there
anyway
(i.e. assigning an attribute etc) to instruct the server to remember a
variables state *without* having to go through the rigmarole of saving
and
loading to and from the Session state manually or similar workaround for
any
Types (including custom types) in exactly the same way web controls
retain
state. I understand the reasons (performance) that this is not done by
default ... but it is really anoying.
e.g. This would be great...
[ViewState save=true]
protected Person myperson;
private void Page_Load(object sender, System.EventArgs e) {
if (!this.IsPostBack)
myperson = new Person("Mr Unhappy");
}
//otherwise myperson still points to an instance
}
so that on every postback myperson instance would still exist.
Any ideas are extremely welcome.
Br,
Mark.