设置并获取一个asp Listbox作为会话

本文关键字:Listbox asp 会话 一个 获取 设置 | 更新日期: 2023-09-27 17:58:46

我有一个asp列表框,它根据用户选择的内容生成项目。这很好用。我的问题是,如何将此列表框存储为会话并检索会话?

设置并获取一个asp Listbox作为会话

要在会话中存储内容。。。

Session["MyKey"] = "My Value";

从会话中检索内容

string myValue = Session["MyKey"];

如果不使用字符串,则可能需要根据需要进行强制转换。

那么你可能会做什么…

//store
Session["SelectedValue"] = MyListBox.SelectedValue;
//later retrieve
string selectedValue = Session["SelectedValue"];

我不建议将列表框本身存储在Session变量中。请尝试将生成的项目存储在会话中。以下是在Session中存储列表的示例。

private List<object> _items;
public List<object> Items
{
    get
    {
        if (_items == null)
        {
            _items = Session["ListBoxItems"] as List<object>;
        }
        return _items;
     }
    set
    {
         Session["ListBoxItems"] = value;            
    }
}