我已经建立了一个简单的购物车。问题是我第一次尝试添加项目时没有任何反应。如果我再次单击购买按钮添加项目。 如果这个问题是什么原因?
感谢名单!
的.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();
}
}
答案 0 :(得分:1)
根据我对第一个答案的评论..试试这个
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,并在SaveViewState()中保存_FlowerList的值
答案 1 :(得分:0)
好的,我猜这里是基于略读代码,所以我可能错了,但也许第一次不是回发而且FillRepeater没有被调用。