如何在Session状态中添加List
有没有办法在会话中添加List? 或者在其他页面中传递List值的任何其他方法?
List ast = new List (); ast.Add("asdas!"); Session["stringList"] = ast; List bst = (List )Session["stringList"];
如果这就是你要问的话,你可以做这些事情。
Session["key"] = List;
以及
myStrings = (List)Session["key"];
您可能希望为HttpSessionState类探索以下两种扩展方法。
public static System.Nullable GetValue (this HttpSessionState session, string key) where T : struct, IConvertible { object value = session[key]; if (value != null && value is T) { return (T)value; } else return null; } public static T GetValue (this HttpSessionState session, string key, T defaultValue) where T : class { object value = session[key] ?? defaultValue; if (value != null && value is T) { return (T)value; } else return default(T); }
前者用于值类型,后者用于参考类型。
用法如下:
int? _customerId = Session.GetValue("CustomerID"); Customer _customer = Session.GetValue("CurrentCustomer", null);
是。
var myList=(List)Session["toList"];