我有一个“AddItem”按钮,用于将商品添加到购物车。一切正常,但如果购物车中的商品没有更新,标签女巫会显示数字,除非我刷新页面。我尝试了很多东西,但似乎没有任何工作。我有两个问题,如何更新标签,以及如何在之前的页面上更新它?例如,当用户单击后退按钮时,标签应显示更新的值。
母版页
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<span class="myBag"> My Bag (<asp:Label id="numItems" runat="server"></asp:Label>)
</span>
</ContentTemplate>
</asp:UpdatePanel>
母版页,代码隐藏
int numItems = cartAccess.numItemsInCart();
Label numItem = (Label)this.FindControl("numItems");
numItem.Text = numItems.ToString();
添加页面
<asp:UpdatePanel ID="UpdateItem" runat="server">
<ContentTemplate>
<asp:Button ID="addToCart" CssClass="addToCart" Text="Add to Shopping Bag" runat="server"/>
</ContentTemplate>
</asp:UpdatePanel>
添加页面,代码
private void adding(object sender, CommandEventArgs e)
{
DataClassesDataContext context = new DataClassesDataContext();
string itemid = e.CommandArgument.ToString();
string quntity = "1";
CartAccess test = new CartAccess();
test.add(itemid, quntity);
ContentPlaceHolder pl = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
UpdatePanel up = (UpdatePanel)pl.FindControl("UpdatePanel1");
AsyncPostBackTrigger tr = new AsyncPostBackTrigger();
tr.ControlID = ((Control)sender).ID;
tr.EventName = "Click";
up.Triggers.Add(tr);
Label numDisplay = (Label)pl.FindControl("numItems");
int numItem = (from item in context.ShoppingCarts select item).Count();
numDisplay.Text = numItem.ToString();
}
答案 0 :(得分:0)
尝试添加
的AutoPostBack = “真”
<asp:Label id="numItems" AutoPostBack="true" runat="server"></asp:Label>
<asp:Button ID="addToCart" AutoPostBack="true" CssClass="addToCart" Text="Add to Shopping Bag" runat="server"/>