DotNet版 - A Question about Session State in ASP.NET |
|
|
|
|
|
d*****t 发帖数: 17 | 1 Hi, All,
I have been confused by a seemingly simple question for a few days. Hope
anyone could give me some suggestion.
What bothers me is that ASP.NET seems to be able to remember the state of an
object after I put that object into the Session property. As you can find
in my code, I didn't explicitly save the object back to the Session property
after I changed it. But the state of the object seems to be updated in
following requests. Did I miss anything here? Thanks in advance.
Below is my code | l*s 发帖数: 783 | 2 This is how it suppose to work.
You are storing the arraylist object in the session and it's going to be the
'same' object whenever you retrieve it afterwards. And this arraylist
object has the references to the string items.
an
property
【在 d*****t 的大作中提到】 : Hi, All, : I have been confused by a seemingly simple question for a few days. Hope : anyone could give me some suggestion. : What bothers me is that ASP.NET seems to be able to remember the state of an : object after I put that object into the Session property. As you can find : in my code, I didn't explicitly save the object back to the Session property : after I changed it. But the state of the object seems to be updated in : following requests. Did I miss anything here? Thanks in advance. : Below is my code
| d*****t 发帖数: 17 | 3 Thanks for your answer. So it seems that what Session has is a reference to
the original object (not a clone of the object), which explains why no
explicit saving back of the object is necessary. I did try to manipulate
variables of primitive data types (such as int) in Session in the same way
but I had to save the variable back to Session every time after I changed it
. It is strange that none of the books I went through mentioned this matter,
neither does MSDN (or maybe I just didn't find it).
【在 l*s 的大作中提到】 : This is how it suppose to work. : You are storing the arraylist object in the session and it's going to be the : 'same' object whenever you retrieve it afterwards. And this arraylist : object has the references to the string items. : : an : property
| l*s 发帖数: 783 | 4 This is not specific to session object. You need to read about value type/
reference type;boxing/unboxing;pass by value/pass by reference
to
it
matter,
【在 d*****t 的大作中提到】 : Thanks for your answer. So it seems that what Session has is a reference to : the original object (not a clone of the object), which explains why no : explicit saving back of the object is necessary. I did try to manipulate : variables of primitive data types (such as int) in Session in the same way : but I had to save the variable back to Session every time after I changed it : . It is strange that none of the books I went through mentioned this matter, : neither does MSDN (or maybe I just didn't find it).
| d*****t 发帖数: 17 | 5 My initial impression was that when objects were stored in a Session, their
clones were placed in a domain (such as SQL Server) separate from the
application domain. This is why I posted my original question. But now it
seems that only references are saved in session, even if an out-of-process
mode is used in Session. A
【在 l*s 的大作中提到】 : This is not specific to session object. You need to read about value type/ : reference type;boxing/unboxing;pass by value/pass by reference : : to : it : matter,
| s******g 发帖数: 120 | 6 Not sure what your question is. Do you want to share what your expected page
result is?
To me, your code is clear as: session stores an array of string values. as
long as it is postback, the session value won't be cleaned up. So if you
enter ABC, click button and then enter DEF, click button, it will show:
ABC
DEF
what was the result you were expecting from your original impression? |
|
|
|
|
|
|