首页 > 代码库 > ASP.NET页面之间传递值(2):Session

ASP.NET页面之间传递值(2):Session

想必这个肯定是大家使用中最常见的用法了,其操作与Application类似,作用于用户个人,所以,过量的存储会导致服务器内存资源的耗尽。

  优点:1.使用简单,不仅能传递简单数据类型,还能传递对象。
     2.数据量大小是不限制的。

  缺点:1.在Session变量存储大量的数据会消耗较多的服务器资源。

     2.容易丢失。

  使用方法:1.在源页面的代码中创建你需要传递的名称和值构造Session变量:Session["Name"]="Value(Or Object)";

       2.在目的页面的代码使用Session变量取出传递的值。Result = Session["Nmae"]

  注意:session不用时可以销毁它,销毁的方法是:清除一个:Session.Remove("session名");

                         清除所有:Session.Clear();

indextest.aspx后台页面:

 1 using System; 2 using System.Collections.Generic; 3 using System.Web; 4 using System.Web.UI; 5 using System.Web.UI.WebControls; 6  7 public partial class JajaWeixinQianduanWeb_WoYaoDingCan_indextest : System.Web.UI.Page 8 { 9     protected void Page_Load(object sender, EventArgs e)10     {11 12     }13 14     protected void btn_chuanzhi_Click(object sender, EventArgs e)15     {16         Session["name"] = this.txt_chuanzhi.Value;17         string s_url = "indextestlist.aspx";18         Response.Redirect(s_url);19     }20 }

indextestlist.aspx后台页面:

 1 using System; 2 using System.Collections.Generic; 3 using System.Web; 4 using System.Web.UI; 5 using System.Web.UI.WebControls; 6  7 public partial class JajaWeixinQianduanWeb_WoYaoDingCan_indextestlist : System.Web.UI.Page 8 { 9     protected void Page_Load(object sender, EventArgs e)10     { 11         string name;12         name = Session["name"].ToString();13         Response.Write(name);14     }15 }