2012-10-25 26 views
1

我想添加會話[「i」的]用的詞添加到會話ASP.NET C#

目前它將只顯示了一組結果

例如的School1 11/10/2011 14/11/2011 GCSE AAA

我想補充多套,但他們似乎並沒有被獲得存儲在Session

例如

School1 11/10/2011 14/11/2011 GCSE AAA

School2 2012年11月10日14/11/2012 ALevels AAA

Education addResults = new Education(schoolName, fromDate, toDate , qualification , grades); 

Session["i"] = (addResults); 

//schoolarraylist.Add(addResults); 

foreach (Education currentschool in schoolarraylist) 
     { 
     Session["i"] = currentschool.Schoollocation + "," + currentschool.Datefrom + "," + currentschool.Dateto + "," + currentschool.Qualifications + "," + currentschool.Grade + "<br />"; 

      string tmp = Session["i"].ToString(); 
      string[] sb = tmp.Split(','); 
      string [] ii = new string[sb.GetUpperBound(0) + 1]; 

      for (int i = 0; i <= sb.GetUpperBound(0); i++) 
       { 
        ib[i] = (sb[i]); 
       } 

      foreach (string j in ii) 
       { 
        Response.Write(ii); 
       } 

      } 

回答

5

您可以指定對象的名單和會議以後再拿回來。 But you should not put data in seesion without need。會話在每個用戶的服務器端進行維護,並且將會話中的數據放在服務器的內存中,這可能會降低應用程序的性能。它在使用前值得閱讀sessions

List<string> lst = new List<string>(); 

Session["i"] = lst; 

從會話對象返回列表。

List<string> lst = (List<string>)Session["i"]; 
0

的問題是,您分配東西會話[「我」],當你嘗試的東西添加到會話,你實際上覆蓋您以前的值。爲了將對象添加到會話中,您必須選擇另一個名稱,例如Session [「j」]或者在你的對象(List,Array,Dictionary等)周圍封裝某種容器並將該容器存儲在Session中。

也嘗試爲您的Sessions找到更好的名稱,如果稍後再查看您的代碼,您可能不會知道Session [「i」]實際上應該是什麼。

0

你也可以使用一個ArrayList的:

ArrayList list = new ArrayList(); 
foreach (Education currentschool in schoolarraylist) 
{ 
    list.Add(currentschool.Schoollocation + "," + currentschool.Datefrom + "," + currentschool.Dateto + "," + currentschool.Qualifications + "," + currentschool.Grade) 
} 

然後遍歷你想whatecver格式ArrayList和顯示器來顯示