J
John
I get the feeling this is a pretty classic problem, but I'm a bit of an uber
newber. Apologies!
Products page, user enters a quantity and clicks one of my "Add to Cart"
buttons, which bubbles up through the datalist and calls a method to
AddToCart(cartID,prodID,prodQty). Now, if the user clicks 'refresh', that
method is executed again without clicking the button. So if they add 2
apples, refresh, they have 4 apples, refresh, 6 apples, etc. I enable
tracing and I can see I've got a form variable that's holding onto the
quantity value- how do I clear it after executing the method?
I've seen this solved by passing the values to another aspx page that then
redirects back to the products page - a little bit classic-ASP style? I'd
prefer not to do that, in the spirit of .Net, and stick to PostBacks, but
I'm not quite sure how to do this. The code I've tried to use to clear the
form values must not be quite right.
Here's the code I'm working with, minus failed fix-attempts, if it makes it
easier to correct me (feel free to correct anything else I'm doing wrong -
my coding needs help). Thanks in advance!!! -John
-----------
private void DataList1_ItemCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
int prodID =
Int32.Parse(DataList1.DataKeys[e.Item.ItemIndex].ToString());
TextBox qtyField = (TextBox)e.Item.FindControl( "txtQty" );
int prodQty = Int32.Parse(qtyField.Text);
ShoppingCart cart = new ShoppingCart();
string cartID = cart.GetCartId();
try
{
if(quantity > 0)
{
qtyField.Text = "0";
cart.AddToCart(cartID, prodID,prodQty); //quantity non-int!? fix
}
}
catch
{
//TODO: update for problem adding item to database
}
}
newber. Apologies!
Products page, user enters a quantity and clicks one of my "Add to Cart"
buttons, which bubbles up through the datalist and calls a method to
AddToCart(cartID,prodID,prodQty). Now, if the user clicks 'refresh', that
method is executed again without clicking the button. So if they add 2
apples, refresh, they have 4 apples, refresh, 6 apples, etc. I enable
tracing and I can see I've got a form variable that's holding onto the
quantity value- how do I clear it after executing the method?
I've seen this solved by passing the values to another aspx page that then
redirects back to the products page - a little bit classic-ASP style? I'd
prefer not to do that, in the spirit of .Net, and stick to PostBacks, but
I'm not quite sure how to do this. The code I've tried to use to clear the
form values must not be quite right.
Here's the code I'm working with, minus failed fix-attempts, if it makes it
easier to correct me (feel free to correct anything else I'm doing wrong -
my coding needs help). Thanks in advance!!! -John
-----------
private void DataList1_ItemCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
int prodID =
Int32.Parse(DataList1.DataKeys[e.Item.ItemIndex].ToString());
TextBox qtyField = (TextBox)e.Item.FindControl( "txtQty" );
int prodQty = Int32.Parse(qtyField.Text);
ShoppingCart cart = new ShoppingCart();
string cartID = cart.GetCartId();
try
{
if(quantity > 0)
{
qtyField.Text = "0";
cart.AddToCart(cartID, prodID,prodQty); //quantity non-int!? fix
}
}
catch
{
//TODO: update for problem adding item to database
}
}