1

我想添加一个具有更多结果的 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 日 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);
                }

            }
4

3 回答 3

5

您可以将对象列表分配给会话,然后再将其取回。But you should not put data in seesion without need. 会话在服务器端为每个用户维护,将数据放入会话中会占用服务器的内存,这可能会降低应用程序的性能。在使用会话之前,值得一读。

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

Session["i"] = lst;

从会话对象中获取列表。

List<string> lst = (List<string>)Session["i"];
于 2012-10-25T07:07:33.530 回答
0

问题是,您为 Session["i"] 分配了一些东西,当您尝试向会话中添加一些东西时,您实际上会覆盖您以前的值。为了将对象添加到 Session,您要么必须选择另一个名称,例如 Session["j"],要么在对象周围包装某种容器(列表、数组、字典等)并将该容器存储在 Session 中。

还要尝试为您的 Sessions 找到更好的名称,如果您稍后查看您的代码,您可能不会知道 Session["i"] 实际上应该是什么。

于 2012-10-25T07:10:25.900 回答
0

您还可以使用 ArrayList :

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

然后遍历arraylist并以您想要显示的任何格式显示

于 2012-10-25T07:13:39.037 回答