经验之谈:如何使用sessionC#开发工程师
/ 福建师范大学电气工程与自动化学院C#开发工程师 / 2017-05-07
我是C#的学者个人感觉:
Session是保存在服务器端的用户变量。可以在一个页面中对Session进行赋值,在另一个页面里访问它。
Session可以用这样的附值方法:
int userId = 10;
string userName = "mytest";
string userPwd = "e12312gfhghghjjy"
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,就可以通过WebForm2.aspx看到之前在WebForm1.cs中为Session变量附的值了。
C#的学习主要是靠积累,看再多代码也不如自己敲理解来的快,汇鱼人才每天会发布更多有用的学习资讯,来帮助你们快速掌握,想要了解更多就关注汇鱼人才吧


Act师大校友联盟
1914篇文章
大家好,我是小明,请多多指教!
热文榜单