我需要创建一个循环,以便当我尝试从购物车中得出小计时,它会继续检查购物车中是否有更多商品。我的购物车此时是一个列表框。我已经将我的小数声明为单价,以便从我的访问表中提取数据。我还需要加上运费(每件 2 美元)、税金(@ 7%),最后合计一下。我是一个完整的新手,任何帮助将不胜感激。我在 Visual Studio 2010 中使用 c# 和 asp.net 的组合。来自购物车页面的 C# 代码
public partial class _Default : System.Web.UI.Page
{
private CartItemList cart;
protected void Page_Load(object sender, EventArgs e)
{
cart = CartItemList.GetCart();
if (!IsPostBack)
this.DisplayCart();
}
private void DisplayCart()
{
lstBox.Items.Clear();
CartItem item;
for (int i = 0; i < cart.Count; i++)
{
item = cart[i];
lstBox.Items.Add(item.Display());
}
}
protected void clrBtn_Click(object sender, EventArgs e)
{
if (cart.Count > 0)
{
cart.Clear();
lstBox.Items.Clear();
}
}
protected void rmvBtn_Click(object sender, EventArgs e)
{
if (cart.Count > 0)
{
if (lstBox.SelectedIndex > -1)
{
cart.RemoveAt(lstBox.SelectedIndex);
this.DisplayCart();
}
else
{
lblMessage.Text = "Please select the item you want to remove.";
}
}
}
protected void checkoutBtn_Click(object sender, EventArgs e)
{
}
}
C#中订单页面的代码
public partial class _Default : System.Web.UI.Page
{
private Product selectedProduct;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
ddlproducts.DataBind();
selectedProduct = this.GetSelectedProduct();
lblName.Text = selectedProduct.Name;
lblShortDescription.Text = selectedProduct.ShortDescription;
lblLongDescription.Text = selectedProduct.LongDescription;
lblUnitPrice.Text = selectedProduct.UnitPrice.ToString("c");
}
private Product GetSelectedProduct()
{
DataView BooksTable = (DataView)
AccessDataSource2.Select(DataSourceSelectArguments.Empty);
BooksTable.RowFilter =
"ProductID = '" + ddlproducts.SelectedValue + "'";
DataRowView row = (DataRowView)BooksTable[0];
Product p = new Product();
p.ProductID = row["ProductID"].ToString();
p.Name = row["Name"].ToString();
p.ShortDescription = row["ShortDescription"].ToString();
p.LongDescription = row["LongDescription"].ToString();
p.UnitPrice = (decimal)row["UnitPrice"];
//p.ImageFile = row["Image File"].ToString();
return p;
}
protected void addBtn_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
CartItemList cart = CartItemList.GetCart();
CartItem cartItem = cart[selectedProduct.ProductID];
if (cartItem == null)
{
cart.AddItem(selectedProduct, 1);
}
else
{
cartItem.AddQuantity(1);
}
Response.Redirect("Cart.aspx");
}
}
}
c#中的caritemlist代码(顺便说一句,我从我的书中复制了所有这些代码。我不知道如何编程。)
public class CartItemList
{
private List<CartItem> cartItems;
public CartItemList()
{
cartItems = new List<CartItem>();
}
public int Count {
get { return cartItems.Count; }
}
public CartItem this[int index]
{
get { return cartItems[index]; }
set { cartItems[index] = value; }
}
public CartItem this[string id]
{
get {
foreach (CartItem c in cartItems)
if (c.Product.ProductID == id) return c;
return null;
}
}
public static CartItemList GetCart()
{
CartItemList cart = (CartItemList) HttpContext.Current.Session["Cart"];
if (cart == null)
HttpContext.Current.Session["Cart"] = new CartItemList();
return (CartItemList) HttpContext.Current.Session["Cart"];
}
public void AddItem(Product product, int quantity)
{
CartItem c = new CartItem(product, quantity);
cartItems.Add(c);
}
public void RemoveAt(int index)
{
cartItems.RemoveAt(index);
}
public void Clear()
{
cartItems.Clear();
}
}