简体   繁体   English

添加到会话ASP.NET C#

[英]Adding to a Session ASP.NET C#

I would like to add a Session["i"] with more results 我想添加一个具有更多结果的Session [“ i”]

currently it will only shows one set of results 目前只会显示一组结果

eg School1 11/10/2011 14/11/2011 GCSE AAA 例如School1 11/10/2011 14/11/2011 GCSE AAA

I would like to add more sets but they do not seem to be getting stored in the Session 我想添加更多的集合,但是它们似乎并没有存储在Session中

eg 例如

School1 11/10/2011 14/11/2011 GCSE AAA School1 2011年11月10日2011年11月14日GCSE AAA

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

            }

You can assign list of object to session and later get it back. 您可以将对象列表分配给会话,然后再将其取回。 But you should not put data in seesion without need . But you should not put data in seesion without need Session are maintained on server side for each user and putting data in session takes memory of server and it is could degrade the performance of the application. 会话是在服务器端为每个用户维护的,将数据放入会话会占用服务器的内存,这可能会降低应用程序的性能。 Its worth reading about sessions before using them. 值得在使用会话之前阅读有关会话的信息

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

Session["i"] = lst;

Getting list back from session object. 从会话对象获取列表。

List<string> lst = (List<string>)Session["i"];

The Problem is, that you assign something to Session["i"] and when you try to add something to the session, you actually overwrite your previous value. 问题是,您为Session [“ i”]分配了一些内容,并且当您尝试向会话中添加内容时,实际上覆盖了您以前的值。 In order to add objects to the Session, you either have to chose another name eg Session["j"] or wrap some sort of container around your objects (List, Array, Dictionary, etc.) and store that container in your Session. 为了将对象添加到会话中,您必须选择另一个名称,例如Session [“ j”],或在对象周围包装某种容器(列表,数组,字典等),然后将该容器存储在Session中。

Also try to find better names for your Sessions, if you take a look at your code at a later point, you probably won't know what Session["i"] is actually supposed to be. 也请尝试为您的Session查找更好的名称,如果稍后再看代码,您可能将不知道Session [“ i”]的实际含义。

You can also use a ArrayList : 您也可以使用ArrayList:

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

Then loop through the arraylist and display in whatecver format you want to display 然后遍历数组列表并以您要显示的格式显示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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