ASP.NET頁面之間傳遞值(2):Session

來源:互聯網
上載者:User

標籤:style   blog   color   使用   os   資料   

想必這個肯定是大家使用中最常見的用法了,其操作與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 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.