Существует ли способ добавить Список на сессии? или какой-либо другой способ передать значения Списка на другой странице?
List<string> ast = new List<string>();
ast.Add("asdas!");
Session["stringList"] = ast;
List<string> bst = (List<string>)Session["stringList"];
Вы можете делать такие вещи, если об этом просите.
Session["key"] = List<string>;
, а также
myStrings = (List<string>)Session["key"];
Возможно, вы захотите изучить следующие два метода расширения для класса HttpSessionState.
public static System.Nullable<T> GetValue<T>(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<T>(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<int>("CustomerID");
Customer _customer = Session.GetValue<Customer>("CurrentCustomer", null);