I have built a simple shopping cart. The problem is that the first time i try to add an item nothing happens. If i click the buy button a second time an item is added. What is the cause if this problem?
Thanx!
.aspx
<asp:Content ContentPlaceHolderID="main" runat="server">
White lily
<asp:Button ID="button" runat="server" OnClick="buy_Click" />
</asp:Content>
<asp:Content ContentPlaceHolderID="rightBar" runat="server">
<UserControl:ShoppingCart id="shoppingCart" runat="server" />
</asp:Content>
.ascx
public partial class UserControls_ShoppingCart : System.Web.UI.UserControl
{
private List<Flower> FlowerList
{
get
{
List<Flower> tmp = Session["FlowerList"] as List<Flower>;
if (tmp == null)
{
tmp = new List<Flower>();
}
return tmp;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillRepeater();
}
}
public void ShoppingCart_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item != null)
{
Label itemLabel = e.Item.FindControl("itemLabel") as Label;
Flower flower = e.Item.DataItem as Flower;
if (flower != null)
{
itemLabel.Text = flower.getName(flower);
}
}
}
private void FillRepeater()
{
shoppingCartRepeater.DataSource = FlowerList;
shoppingCartRepeater.DataBind();
}
public void AddFlower(Flower flower)
{
FlowerList.Add(flower);
Session["FlowerList"] = FlowerList;
FillRepeater();
}
}