[英]Store information using session variable
I'm new to ASP.NET :) and I'd like to understand more about session
. 我是ASP.NET的新手:),我想了解更多有关
session
。 Here's a simple example: Every time I click the button it will add one more integer to listInt
and I store the list using Session["listInt"]
. 这是一个简单的示例:每次单击按钮时,都会向
listInt
添加一个整数, listInt
使用Session["listInt"]
存储列表。
public partial class TestSession : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["listInt"] == null)
{
Session["listInt"] = new List<Int16>();
}
}
}
protected void AddInt_Click(object sender, EventArgs e)
{
Int16 i = 0;
List<Int16> listInt = (List<Int16>)Session["listInt"];
listInt.Add(i);
Session["listInt"] = listInt;
Response.Write("Hello!");
}
}
Here's the thing I don't understand, if I comment the line Session["listInt"] = listInt;
如果我注释
Session["listInt"] = listInt;
行,这就是我不明白的事情Session["listInt"] = listInt;
, whenever I click the variable Session["listInt"]
still store the value (means still add more integer to the list): ,每当我单击变量
Session["listInt"]
仍会存储该值(意味着仍将更多整数添加到列表中):
Int16 i = 0;
List<Int16> listInt = (List<Int16>)Session["listInt"];
listInt.Add(i);
//Session["listInt"] = listInt; //No idea why....
Response.Write("Hello!");
Can anyone please tell me how session works in this case? 有人可以告诉我在这种情况下会话如何工作吗? Thanks in advance :)
提前致谢 :)
Your list is a reference type, so when you retrieve it from the server via the session state container you actually get a reference to some object in the server memory. 您的列表是引用类型,因此当您通过会话状态容器从服务器检索它时,您实际上会获得对服务器内存中某个对象的引用。 Hence no need to reassign it later.
因此,以后无需重新分配它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.