繁体   English   中英

添加到会话ASP.NET C#

[英]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);
                }

            }

您可以将对象列表分配给会话,然后再将其取回。 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"];

问题是,您为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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM