从购物车vb.net中删除商品

时间:2011-04-07 12:52:00

标签: vb.net

您好 我已经在vb.net中创建了一个购物车,到目前为止我可以将商品添加到购物车但不能清空或从购物车中删除商品。 有人可以帮我吗?

这是我到目前为止的代码: -

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">

 <div id="mycart">
        <br />
    Your shopping cart:<br />
    <table style="width: 500px" align="center" cellspacing="0" 
            cellpadding="0" border="0">
        <tr>
            <td style="width: 286px; height: 153px">
                <asp:ListBox ID="lstCart" runat="server"
                    Width="267px" Height="135px">
                </asp:ListBox>
            </td>
            <td style="height: 153px">
                <asp:Button ID="btnRemove" runat="server"
                    Width="100px" Text="Remove Item" /><br /><br />
                <asp:Button ID="btnEmpty" runat="server" 
                    Width="100px" Text="Empty Cart" />
            </td>
        </tr>
    </table>
    <br />
    <asp:Button ID="btnContinue" runat="server"
        PostBackUrl="~/index.aspx" Text="Continue Shopping" />&nbsp;
    <asp:Button ID="btnCheckOut" runat="server" PostBackUrl="~/CheckOut.aspx" Text="Check Out" /><br />
    <br />
    <asp:Label ID="lblMessage" runat="server"></asp:Label>
</div>

Partial Class MyCart
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load#
    Dim myCart = ShoppingCart.Instance
    lstCart.Items.Clear
    For Each item As CartItem In myCart.Items
        lstCart.Items.Add(item.Description)
    Next

End Sub

结束班

1 个答案:

答案 0 :(得分:0)

应该只是你已经得到的减去每个,即:

lstCart.Items.Clear 

编辑:刚刚注意到HTML中的按钮,为了让它们正常工作,您需要以下内容:

    Protected Sub btnRemove_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        lstCart.Items.Remove(lstCart.SelectedItem)
    End Sub

    Protected Sub btnEmpty_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEmpty.Click
        lstCart.Items.Clear 
    End Sub