[英]Adding to a Session ASP.NET C#
我想添加一個具有更多結果的Session [“ i”]
目前只會顯示一組結果
例如School1 11/10/2011 14/11/2011 GCSE AAA
我想添加更多的集合,但是它們似乎並沒有存儲在Session中
例如
School1 2011年11月10日2011年11月14日GCSE AAA
School2 2012年11月10日2012年11月14日AAAA級
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);
}
}
問題是,您為Session [“ i”]分配了一些內容,並且當您嘗試向會話中添加內容時,實際上覆蓋了您以前的值。 為了將對象添加到會話中,您必須選擇另一個名稱,例如Session [“ j”],或在對象周圍包裝某種容器(列表,數組,字典等),然后將該容器存儲在Session中。
也請嘗試為您的Session查找更好的名稱,如果稍后再看代碼,您可能將不知道Session [“ i”]的實際含義。
您也可以使用ArrayList:
ArrayList list = new ArrayList();
foreach (Education currentschool in schoolarraylist)
{
list.Add(currentschool.Schoollocation + "," + currentschool.Datefrom + "," + currentschool.Dateto + "," + currentschool.Qualifications + "," + currentschool.Grade)
}
然后遍歷數組列表並以您要顯示的格式顯示
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.