【问题标题】:Storing and retrieving multiple values in a single session在单个会话中存储和检索多个值
【发布时间】:2013-08-28 15:51:43
【问题描述】:

我知道有些主题的标题相似,但这有点不同。

首先,为了在单个会话中存储多个值,我必须使用List 而我将列表与会话中的值一起存储,对吗?

如果是这样,当我想向已经在会话中的列表添加值时,我会从会话中检索列表并添加值。但是每次我从List 添加/删除一个值时,我是否需要将列表分配回会话?或者,默认情况下,当我操作它时,它会在会话中自动更新,因为它最初是在会话中分配的,然后是之后的。

更新:提供我的问题的示例代码

public void assignNewID(int currentID)
{
    if(Session["usersID"] == null)
    {
        Session["usersID"] = new List<int>();
    }

    if(currentID != null)
    {
        var list = (List<int>)Session["usersID"];
        list.Add(currentID);

        // now should I hereby assign the list back to
        // the session like:
        // Session["usersID"] = list;
        // or it gets automatically updated in the session by default ?
    }
}

【问题讨论】:

  • 通过发布实际的List 声明和添加到会话的代码使其更精确。
  • @HenkHolterman - 我用示例代码更新了我的帖子。 :)

标签: c# asp.net list session


【解决方案1】:

List 是一种引用类型,因此您将 reference 存储到您的会话中,如果对象被更新,它将被更新,

【讨论】:

    【解决方案2】:

    这里有个问题。

    您的代码没问题,不需要重新分配列表(不是那么麻烦)。

    但只要您在 1 个服务器 (&lt;sessionstate mode="inproc" /&gt;) 上运行它。

    如果将其扩展到多台服务器,您将遇到List&lt;&gt; 未标记为[Serializable] 的问题。您的解决方案不会直接起作用。

    一个简短的解决方法是使用:

    [Serializable]
    class MyList : List<int>
    {
    }
    

    【讨论】:

      【解决方案3】:

      试试这个:

      // Saving in session
                  System.Collections.Hashtable ht = new System.Collections.Hashtable();
                  ht.Add("EmployeeName", "EmpName Value");
                  ht.Add("Designation", "Designation Value");
                  ht.Add("Department", "Department Value");
                  Session["EmployeeInfo"] = ht;
                  //Retrieve from session
                  if (Session["EmployeeInfo"] != null)
                  {
                      string strEmployeeName = ht.ContainsKey("EmployeeName") ? Convert.ToString(ht["EmployeeName"]) : "";
                      string strDesignation = ht.ContainsKey("Designation") ? Convert.ToString(ht["Designation"]) : "";
                      string strDepartment = ht.ContainsKey("Department") ? Convert.ToString(ht["Department"]) : "";
                  }
      

      【讨论】:

        【解决方案4】:

        看这个例子

        public static class SessionManager
        {
            public static List<Entity.Permission> GetUserPermission
            {
                get
                {
                    if (HttpContext.Current.Session["GetUserPermission"] == null)
                    {
                        //if session is null than set it to default value
                        //here i set it 
                        List<Entity.Permission> lstPermission = new List<Entity.Permission>();
                        HttpContext.Current.Session["GetUserPermission"] = lstPermission;
                    }
                    return (List<Entity.Permission>)HttpContext.Current.Session["GetUserPermission"];
                }
                set
                {
                    HttpContext.Current.Session["GetUserPermission"] = value;
                }
            }
         }
        

        无论如何现在

            protected void chkStudentStatus_CheckedChanged(object sender, EventArgs e)
            {
                Entity.Permission objPermission=new Entity.Permission();
                SessionManager.GetUserPermission.Add(objPermission);
                SessionManager.GetUserPermission.RemoveAt(0);
        
        
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-05-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-31
          • 2013-08-01
          相关资源
          最近更新 更多