当我单击此LinkButton时,我需要将对象保存在列表中,但是如果再次单击,列表将丢失旧值,并且列表计数= 1,有什么建议吗?
List<Product> products = new List<Product>();
protected void AddProduct_Click(object sender, EventArgs e)
{
int productID = Convert.ToInt32((sender as LinkButton).CommandArgument); /*Pega o id do button que foi clicado relativa a reserva*/
products.Add(ProductBLL.GetProductByID(productID));
ViewState["products"] = products;
}
答案 0 :(得分:1)
我能够通过从ViewState检索列表(如果存在),然后将其添加到其中来解决此问题。
protected void AddProduct_Click(object sender, EventArgs e)
{
List<Product> products = new List<Product>();
if(ViewState["products"] != null)
products = (List<Product>) ViewState["products"];
int productID = Convert.ToInt32((sender as LinkButton).CommandArgument);
products.Add(ProductBLL.GetProductByID(productID));
ViewState["products"] = products;
}