|
Hi. I have a problem regarding my shopping cart.When a user adds to cart, he/she should be able to edit the quantity without having to click on the edit button as used in datagrid. How do I go about doing it?
here are my codes:
private void Page_Load(object sender, System.EventArgs e)
{
string ProdID = Request.QueryString["ProductID"];
ArrayList cart;
cart = (ArrayList)Session["cart"];
if(cart==null)
cart = new ArrayList();
DataGrid1.DataSource = cart;
if(!IsPostBack)
{
if(Request.QueryString["ProductID"]!=null)
{
string id = Request.QueryString["ProductID"];
string prodName = Request.QueryString["ProductName"];
decimal price = decimal.Parse(Request.QueryString["Price"]);
//checks if product is already in cart
//bool found = false;
//foreach(cartItem citem in cart)
//{
// if(citem.ProdID.Equals(id))
// {
// found = true;
// citem.Qty++;
// }
//}
//if(!found)
cart.Add(new cartItem(id,prodName,"some desc",price,1));
Session["cart"] = cart;
}
DataGrid1.DataBind();
}
this.RefreshTotal();
}
private void RefreshTotal()
{
lblTotal.Text = this.GetTotal().ToString("C");
}
private decimal GetTotal()
{
ArrayList cart = (ArrayList)Session["cart"];
if(cart==null) return 0;
//calculates grand total
decimal grandTotal = 0;
foreach(cartItem citem in cart)
{
grandTotal += citem.UnitPrice * citem.Qty;
}
return grandTotal;
}
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
DataGrid1.DataBind();
}
private void DataList1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
TextBox txtQty;
txtQty = (TextBox)(e.Item.FindControl("Quantity"));
string newQuantity = txtQty.Text;
// Add code here to update the Quantity value of the data source
// using the newQuantity variable. Note taht newQuantity is a
// string and would have to be converted to a number.
DataGrid1.EditItemIndex = -1;
DataGrid1.DataBind();
}
|
|
|
|
|
|
|
|