目前我对Session的理解是:Session是保存在服务器端的用户变量。我可以在一个页面中对Session进行值,然后在另一个页面里访问它。
Session的附值方法如下:
int userId = 1;
string userName = "test";
string userPwd = "e10adc3949ba59abbe56e057f20f883e"
Session["userId"] = userId;
Session["userName"] = userName;
Session["userPwd"] = userPwd;
或者是使用Session的Add方法
Session.Add("userId",userId);
Session.Add("userName", userName);
Session.Add("userPwd", userPwd);
这有点像是在创建一个哈希表:)
在WebForm1.cs中的Page_Load事件里添加上上面的代码,然后就一在WebForm2.cs的Page_Load事件里添加如下代码:
Lable1.text = Session["userId"].ToString();
Lable2.text = Session["userName"].ToString();
Lable3.text = Session["userPwd"].ToString();
接下来,先打开WebForm1.aspx,然后再打开WebForm2.aspx,就可以看到之前在WebForm1.cs中为Session变量附的值了。
相关文章:
[转] ASP.NET Session详解
代码:将数据从一个 Web 窗体页传递到另一个 Web 窗体页 (Visual C#)