我是asp的新手,所以我觉得答案应该很简单,但我无法弄清楚我做错了什么。这是一个非常简单的购物车练习,我正在学习ASP。我创建会话,添加内容,然后在结帐页面上应该能够逐个从购物车中删除项目。如果单击“删除”,则会从列表框中删除该项目,但不会从会话中删除。我知道它会在会话中持续存在,因为如果我点击购物页面的链接,购物页面上的列表框将填充我认为我删除的项目。
我尝试过两种不同的方法,但我不确定它为什么不起作用。
注意:字典键是用于填充列表框的产品的实际名称。值条目是我现在不实际使用的productID#。购物页面上的列表框正在填充sqlsource中的数据。 SelectedItem.Text =产品名称(显示为什么)和SelectedValue = productID(未使用)。
非常感谢你的帮助
//Default (shopping) page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Create a new collection for the session indexer
ArrayList cart = (ArrayList)Session["scart"];
//Need to initialize a new object in case it's null
if (cart == null)
{
cart = new ArrayList();
Session["scart"] = cart;
}
//Show the shopping car listbox if there are items in it and the
//user returned from the checkout page
else pnlItems.Visible = true;
foreach (DictionaryEntry item in cart)
{
lbItems.Items.Add((string)item.Key);
}
}
//Show the shipping cart listbox containing the items the user just added
if (IsPostBack) pnlItems.Visible = true;
}
private void add_to_cart(string item, int value)
{
//Method to add the selected item to the collection/session indexer
ArrayList cart = (ArrayList)Session["scart"];
cart.Add(new DictionaryEntry(item, value));
}
protected void btnAddItem_Click(object sender, EventArgs e)
{
//Method to send the selected item to the add_to_cart method
string item = ddlProducts.SelectedItem.Text;
lbItems.Items.Add(item);
int value = int.Parse(ddlProducts.SelectedValue);
add_to_cart(item, value);
}
protected void ddlProducts_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void btnCheckOut_Click(object sender, EventArgs e)
{
//Send the user to the checkout page when the button is clicked
Response.Redirect("checkout.aspx", true);
}
}
//CheckoutPage:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Populate the shopping cart on the checkout page with the items in the collection
ArrayList cart = (ArrayList)Session["scart"];
foreach (DictionaryEntry item in cart)
{
lbCheckOut.Items.Add((string)item.Key);
}
}
}
protected void btnKillOrder_Click(object sender, EventArgs e)
{
//Kills the order by ending the session, then returns the user to the shopping page.
Session.Clear();
Response.Redirect("Default.aspx", true);
}
protected void btnRemove_Click(object sender, EventArgs e)
{
//Removes the selected item from the shopping cart listbox and the session indexer
ArrayList cart = (ArrayList)Session["scart"];
cart.Remove(lbCheckOut.SelectedItem.Text);
lbCheckOut.Items.Remove(lbCheckOut.SelectedItem.Text);
Session["scart"] = cart;
//Session.Remove(lbCheckOut.SelectedItem.Text);
}
}
答案 0 :(得分:0)
您确实在会话状态中添加了DictionaryEntry
个实例:
private void add_to_cart(string item, int value)
{
ArrayList cart = (ArrayList)Session["scart"];
cart.Add(new DictionaryEntry(item, value));
}
但是你之后试图删除字符串,这不起作用:
protected void btnRemove_Click(object sender, EventArgs e)
{
ArrayList cart = (ArrayList)Session["scart"];
cart.Remove(lbCheckOut.SelectedItem.Text); // Never matches anything.
}
您应该查找键入字符串的DictionaryEntry
并删除该条目:
protected void btnRemove_Click(object sender, EventArgs e)
{
ArrayList cart = (ArrayList) Session["scart"];
foreach (DictionaryEntry item in cart) {
if ((string) item.Key == lbCheckOut.SelectedItem.Text) {
cart.Remove(item);
break;
}
}
}
答案 1 :(得分:0)
问题是您的Session对象包含DictionaryEntry实例的ArrayList。因此,cart.Remove(lbCheckOut.SelectedItem.Text)
正在尝试将文本与DictionaryEntry实例进行比较,并且每次都返回false。如果你把一个真正的Dictionary实例放在Session而不是ArrayList中会更简单:
if (cart == null)
{
cart = new Dictionary<string,int>();
Session["scart"] = cart;
}
...
var cart = (Dictionary<string,int>)Session["scart"];
cart.Remove( lbCheckOut.SelectedItem.Text );