[英]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.