[英]Problem with shopping cart, asp.net c#
我已經建立了一個簡單的購物車。 問題是我第一次嘗試添加項目時沒有任何反應。 如果我第二次單擊購買按鈕,則會添加一個項目。 如果出現此問題是什么原因?
感謝名單!
的.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();
}
}
根據我對第一個答案的評論。
private List<Flower> FlowerList
{
get
{
return(_FlowerList);
}
set
{
_FlowerList = value;
}
}
protected List<Flower> _FlowerList = new FlowerList();
protected void Page_Load(object sender, EventArgs e)
{
if (Session["FlowerList"]!= null) {
FlowerList = (List<Flower>)Session["FlowerList"];
}
if (!Page.IsPostBack)
{
FillRepeater();
}
}
它應該與其他所有東西都一樣。 更好的方法是使用ViewState,然后將_FlowerList的值保存在SaveViewState()中
好的,我在這里是基於略讀代碼的猜測,所以我可能是錯的,但是也許第一次不是回發,並且不會調用FillRepeater。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.