添加到会话ASP.NET C#

时间:2012-10-25 07:05:50

标签: c# asp.net session

我想添加更多结果的会话[“i”]

目前它只会显示一组结果

e.g。 School1 11/10/2011 14/11/2011 GCSE AAA

我想添加更多集,但它们似乎没有存储在Session

e.g。

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

School2 11/10/2012 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);
                }

            }

3 个答案:

答案 0 :(得分: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"];

答案 1 :(得分:0)

问题是,您为Session [“i”]分配了一些内容,当您尝试向会话添加内容时,实际上会覆盖以前的值。要将对象添加到会话,您必须选择其他名称,例如Session [“j”]或在对象周围包装某种容器(List,Array,Dictionary等)并将该容器存储在Session中。

同时尝试为你的会话找到更好的名字,如果你稍后看一下你的代码,你可能不知道Session [“i”]实际应该是什么。

答案 2 :(得分:0)

您还可以使用ArrayList:

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

然后遍历arraylist并以whatecver格式显示你要显示的内容