这是代码。我也试过使用oncommad属性,但仍然没有运气。
这些图像按钮是asp:repeater的一部分。
我正在将它用于网上商店。
<span class="col col-xs-12 col-md-3">
<asp:RegularExpressionValidator ID="QuantityRegularExpressionValidator" runat="server"
ValidationExpression="\d+" ControlToValidate="QuantityTextBox" ErrorMessage="*"
Display="None"></asp:RegularExpressionValidator>
<asp:RangeValidator ID="QuantityRangeValidator" runat="server"
ErrorMessage="RangeValidator" ControlToValidate="QuantityTextBox"
MaximumValue="9999" MinimumValue="1" Display="None"></asp:RangeValidator>
<asp:ImageButton ID="UpdateItemQuantityImageButton" runat="server"
ImageUrl="~/Responsive/custom/images/Shop/refreshItem.png"
Height="19" OnClick="UpdateItemQuantityImageButton_Click"
CssClass="shopCartListButton" />
<asp:ImageButton ID="DeleteItemImageButton" runat="server"
ImageUrl="~/Responsive/custom/images/Shop/deleteItemFromCart.png"
Height="19" OnClick="DeleteItemImageButton_Click" CssClass="shopCartListButton" />
</span>
Code Behind如下。我已经添加了页面加载和页面初始化事件,如果有帮助找出解决方案。
提前致谢。
protected void Page_Init(object sender, EventArgs e)
{
siteId = Master.siteId;
//check if this page is empty.
if (Master.pageId == Guid.Empty)
{
//this page is unknown, so get the page based on the pageTypeId.
thisPage = DAL.DataClasses.Page.GetPageForSite(pageTypeId, siteId);
}
try
{
shopCartId = long.Parse(Session["shopCartId"].ToString());
thisShopCart = DAL.DataClasses.ShopCart.GetObjectById(shopCartId);
thisShop = thisShopCart.Shop;
}
catch
{
//can't get a product.
//go to the shop front.
thisShop = DAL.DataClasses.Shop.GetDefaultShopForSite(siteId);
if (thisShop != null)
{
Response.Redirect(DAL.DataClasses.Shop.GetDetailPageURL(thisShop.ShopId, siteId));
}
else
{
//no shop for this site. redirect to home page.
Response.Redirect(Master.thisSite.WebsiteURL);
}
}
DisplayContent();
}
protected void Page_Load(object sender, EventArgs e)
{
TransactionTotalLiteral.Text = DAL.DataClasses.ShopCart.CalculateCartTotal(shopCartId).ToString("C");
}
public void DisplayContent()
{
// PageHeadingLiteral.Text = thisShop.Name;
IList<ORM.Shopcartitem> shopCartItemList = DAL.DataClasses.ShopCart.GetItemsInCart(shopCartId);
if (shopCartItemList.Count() == 0)
{
CheckoutButton.Visible = false;
EmptyLiteral.Visible = true;
}
else
{
EmptyLiteral.Visible = false;
}
ShopCartRepeater.DataSource = shopCartItemList;
ShopCartRepeater.DataBind();
}
protected void DeleteItemImageButton_Click(object sender, ImageClickEventArgs e)
{
ImageButton thisImageButton = (ImageButton)sender;
RepeaterItem thisRepeaterItem = (RepeaterItem)thisImageButton.Parent;
Repeater thisRepeater = (Repeater)thisRepeaterItem.Parent;
HiddenField ShopCartItemIdHiddenField = (HiddenField)thisRepeaterItem
.FindControl("ShopCartItemIdHiddenField");
TextBox QuantityTextBox = (TextBox)thisRepeaterItem.FindControl("QuantityTextBox");
long shopCartItemId = long.Parse(ShopCartItemIdHiddenField.Value);
DAL.DataClasses.ShopCart.DeleteItemFromCart(shopCartItemId);
TransactionTotalLiteral.Text = DAL.DataClasses.ShopCart.CalculateCartTotal(shopCartId)
.ToString("C");
DisplayContent();
}