簡體   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