[英]How to move multiple selected items in a ListBox back to another ListBox?
[英]how to store multiple selected listbox items in a session and view on another form?
我想做的是,我想從列表框中選擇1,2或3個項目,並將它們保存到會話中,然后將它們全部顯示在列表框中的另一個窗體上。
這是我的代碼! 這是我關於堆棧溢出的第一篇文章,所以請不要討厭<3
//WebForm1
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lstProducts.Items.Add("Soap");
lstProducts.Items.Add("Schampoo");
lstProducts.Items.Add("Conditioner");
}
}
protected void cmdBuy_Click(object sender, EventArgs e)
{
string[] products = new string[3];
for (int i = 0; i < lstProducts.Items.Count; ++i)
{
if (lstProducts.Items[i].Selected)
products[i] = lstProducts.Items[i].Text;
else
products[i] = "0";
}
Session["Cart"] = products;
}
protected void cmdCart_Click(object sender, EventArgs e)
{
if (Session["Cart"] != null)
{
Response.Redirect("WebForm2.aspx");
}
}
}
//WebForm2
protected void Page_Load(object sender, EventArgs e)
{
string[] products = (string[])Session["Cart"];
for (int i = 0; i < 3; ++i)
{
if (products[i] != "0")
{
lstCart.Items.Add(products[i]);
}
}
}
}
}
事情是,我只得到最后選擇的項目顯示在form2的列表框中???
嘗試這個
要存儲列表框的所有項目,您可以將這些項目添加為數組:
string[] a = new string[]{"item 1","item 2","item 3"};
Session["values"] = a;
在下一頁中,您可以像這樣檢索它。
string[] a = (string[])Session["values"]
編輯#1
你的情況,你可以喜歡
ArrayList al = new ArrayList();
for (int i = 0; i < ListBox1.Items.Count; i++)
{
if (ListBox1.Items[i].Selected == true)
{
al.Add(ListBox1.Items[i].Value);
}
}
Session["selectedValues"] = al;
現在您可以在另一個頁面中使用此sessiom變量,但不要忘記強制轉換為ArrayList類型的對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.