0
static List<string> keywordList = new List<string>();

protected void btnEnter_Click(object sender, EventArgs e)
{
    lbxKeywords.Items.Add(tbxKeyword.Text);
    keywordList.Add(tbxKeyword.Text);
    tbxKeyword.Text = string.Empty;
}

protected void btnSearch_Click(object sender, EventArgs e)
{
    Session["keywords"] = keywordList;
    keywordList.Clear();
    Response.Redirect("Results.aspx");
}

当我清除列表会话时变为空。这是为什么?

谢谢。。

4

1 回答 1

2

那是因为您存储在会话变量中的只是对列表的引用,而不是列表的副本。

为会话变量创建列表的副本:

Session["keywords"] = new List<string>(keywordList);
于 2012-05-08T07:29:20.357 回答