ASP.NET:是否可以保持复杂的对象,如表<>是会话? [英] ASP.NET: Is it possible to keep complex object such as list<> is the session?

查看:155
本文介绍了ASP.NET:是否可以保持复杂的对象,如表<>是会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET:是否有可能保持复杂的对象,如列表<对象> 是会话?如果是的话,该怎么办呢?我试图保持会话列表,但我被告知,我不能在列表转换为字符串。

ASP.NET: Is it possible to keep complex object such as List<object> is the session? If yes, how to do so? I'm trying to keep a list in the session, but I'm being told that I cannot convert that List to a string.

[Serializable]
public class Client
{
        public string ClientType { get; set; }
        public string ClientName { get; set; }
        public string SubClientName { get; set; }
        public string Project { get; set; }
        public string Service { get; set; }
        public string Activity { get; set; }
}
List<Client> ListOfClients;
protected void Page_Load(object sender, EventArgs e)
{
     if(!IsPostBack)
     {
        ListOfClients = new List<Client> { new Client()}; 
        Session["ListOfClients"] = ListOfClients;//This is where the error occurs
                                                 //Cannot convert List<Client> to string... 
     }  
}

有许多操作来执行,但这个想法是让无论是在客户端的会话列表中。

There are many operations to execute, but the idea is to keep whatever is in the list of clients in the session.

感谢您的帮助。

推荐答案

是的,你可以存储任何序列化对象成一个会话变量。例如:

Yes, you can store any serializable object into a session variable. For example:

List<string> list;

Session["list"] = list;

然后返回值:

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

这篇关于ASP.NET:是否可以保持复杂的对象,如表&LT;&GT;是会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆