J
Jonathan Wood
I thought I had this figured out but I'm running into trouble.
In the code below, I update the Quantity property of a CartItem, which is
stored in the Session object. However, in another page when I read this data
back, the change is no longer reflected.
As I understand it, all my assignments are simply copying the pointer to the
actual data. But this code behaves as though I'm copying the data. Can
anyone explain why this might be? Could it have anything to do with the
Serializable attribute?
// In CS file:
[Serializable()]
public class CartItem
{
public int ProductID { get; set; }
public int Version { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
};
// In ASPX file:
public List<CartItem> CartItems
{
get { return (List<CartItem>)ViewState["CartItems"]; }
set { ViewState["CartItems"] = value; }
}
// ...
CartItem ci = CartItems.Find(delegate(CartItem item) { return
item.ProductID == id; });
if (ci != null)
{
if (e.CommandName == "Update")
{
string s = String.Format("txtQuantity{0}", id);
TextBox tb =
(TextBox)tblItems.FindControl(String.Format("txtQuantity{0}", id));
if (tb != null)
{
int qty;
if (int.TryParse(tb.Text, out qty))
{
if (qty > 0)
ci.Quantity = qty;
}
}
}
}
Thanks.
Jonathan
In the code below, I update the Quantity property of a CartItem, which is
stored in the Session object. However, in another page when I read this data
back, the change is no longer reflected.
As I understand it, all my assignments are simply copying the pointer to the
actual data. But this code behaves as though I'm copying the data. Can
anyone explain why this might be? Could it have anything to do with the
Serializable attribute?
// In CS file:
[Serializable()]
public class CartItem
{
public int ProductID { get; set; }
public int Version { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
};
// In ASPX file:
public List<CartItem> CartItems
{
get { return (List<CartItem>)ViewState["CartItems"]; }
set { ViewState["CartItems"] = value; }
}
// ...
CartItem ci = CartItems.Find(delegate(CartItem item) { return
item.ProductID == id; });
if (ci != null)
{
if (e.CommandName == "Update")
{
string s = String.Format("txtQuantity{0}", id);
TextBox tb =
(TextBox)tblItems.FindControl(String.Format("txtQuantity{0}", id));
if (tb != null)
{
int qty;
if (int.TryParse(tb.Text, out qty))
{
if (qty > 0)
ci.Quantity = qty;
}
}
}
}
Thanks.
Jonathan