I need to add a subtotal to my shopping cart, im creating a hashtable then displaying the cart items in a asp:repeater.
here's my code for the hashtable.
can somebody advise me how to do this please?
Thanks
Matt
private void DisplayCartContents(ShoppingCart myCart)
{
//get all items in cart and display
ArrayList cartItems = myCart.getItemsInCart();
errorid.Text = "";
aList.Clear();
lnkBuy.NavigateUrl=myCart.checkOut();
if (cartItems != null)
{
IEnumerator num = cartItems.GetEnumerator();
while (num.MoveNext())
{
Hashtable aCart = new Hashtable();
//get each one..and display
Object i = num.Current;
ShoppingCartItem currentItem = (ShoppingCartItem)i;
aCart.Add("Title",currentItem.Title);
aCart.Add("Price",currentItem.Price);
aCart.Add("Quantity",currentItem.Quantity);
aCart.Add("Itemid",currentItem.CartItemId);
aCart.Add("PurchaseURL",currentItem.PurchaseURL);
aList.Add(aCart);
}
lnkBuy.Visible=true;
}
else
{
lnkBuy.Visible=false;
}
RepeatCart.DataSource=aList;
RepeatCart.DataBind();
}
}
}hi matt,
without running your code ( im not near my dev machine )
i thibk its just a case of declaring a subtotal variable and adding
currentitem.price to it each time you loop thru your enumerator
then displaying the value. possibly in the footer item of the repeater
cheers
funkybeatz
Thanks,
I've managed to get working with this, although its not very pretty and sadly with html labels not asp:
How would i get the subtotal to add the items within the repeater first, before displaying the repeater so as to insert the Subtotal in the header?
Bit of a mouthful,
Tar
m@.
if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
string qty = ((System.Web.UI.HtmlControls.HtmlInputText)e.Item.FindControl("Quantity")).Value;
string prc = ((System.Web.UI.HtmlControls.HtmlInputText)e.Item.FindControl("Price")).Value.Replace("£","");
string total = lblTotalCost.Text.ToString();
lblTotalCost.Text = Convert.ToString(Convert.ToDecimal(total) + (Convert.ToInt32(qty) * Convert.ToDecimal(prc)));
}
alright m@.y, not a scouser by any chance are we? tar!
it shouldnt make any difference i think, cant you just move the label into the header?
if thats no good for any reason the quick and dirty answer would just be to do the subtotal calculation first, in a separate loop, so you have the cost before you even start with the repeater. maybe you should rethink this bit of code tho, there are much more elegant ways of doing this kind of thing, without using hashtables and arraylists. think about datatables.
cheers
Not quite, im just a silly sod from Kent!
I wish it was that simple, still a newbie to .net, and learning from scratch is proving slow.
Im going to put the price in the footer, then a few months down the line come back to it when my knowledge is greater than 1.
Will look at datatables now.
Nice one
m@.
0 comments:
Post a Comment