【问题标题】:storing data for the session without using external database在不使用外部数据库的情况下为会话存储数据
【发布时间】:2014-11-18 10:07:12
【问题描述】:

我正在为基于团队的游戏创建一个 Web 应用程序。这是场景:

    1. First we enter the participants ID. After entering an ID 
       the user clicks save button and the same is displayed in a gridview below. 
       This way we enter multiple IDs and all of them are added in the gridview.

    2. Then we click shuffle and the inserted IDs are shuffled.

我已经有了第二部分的逻辑。但我无法一个接一个地存储 ID。 我想知道为该会话存储 ID 的最佳方式是什么。我不想使用任何外部数据库。我已经尝试过列表、数组、字典、数据表等,但是一旦用户插入 ID 并单击保存,页面重新加载并且新数据覆盖旧数据并且只显示一个 ID在网格视图中。任何人都可以提出实现这一目标的最佳方法吗?

【问题讨论】:

  • 如果不需要持久性,将它们存储在会话中,如果您希望 ID 保留在会话/用户之间而不使用数据库,那么您可以使用文件(可能是 csv 的 ID) .

标签: c# asp.net gridview datatable


【解决方案1】:

您可以创建ArrayList的ID并将其保存在Session

保存

ArrayList Al=new ArrayList();

if(Al.Indexof("23"))         // check items already exists

Al.Add("12");                //adding items
Al.Add("13");
Al.Add("14");

Al.Remove("12")        //remove item

Session["myId"]=Al;

检索

if(Session["myId"]!=null)
ArrayList Al=  (ArrayList) Session["myId"];
else
ArrayList Al=new ArrayList();

【讨论】:

  • 我正在考虑使用外部 csv 文件。但你的方法看起来很棒。谢谢
  • @nitinvertigo 使用集合可以轻松操作它...欢迎您
猜你喜欢
  • 2011-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-22
  • 2016-01-13
  • 1970-01-01
  • 2018-03-08
  • 2019-05-31
相关资源
最近更新 更多