asp:ImageButton 不触发 OnClick 事件 - 它是随机工作的

asp:ImageButton not firing OnClick event- it works randomly

提问人:Satpal Singh 提问时间:12/19/2016 最后编辑:Satpal Singh 更新时间:12/19/2016 访问量:55

问:

这是代码。我也尝试过使用 oncommad 属性,但仍然没有运气。

这些 imagebutton 是 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>

代码隐藏如下。我添加了页面加载和页面初始化事件,如果这有助于找出解决方案。

提前致谢。

        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();
}
C# asp.net aspbutton

评论

1赞 Ambrish Pathak 12/19/2016
“共享图像”按钮的代码隐藏
0赞 Satpal Singh 12/19/2016
请立即查看。如果您需要更多信息,请告诉我。
0赞 Ondrej Svejdar 12/19/2016
对不起,这还不够;虽然提示很少 - 随机 - 这是否意味着它是第一次触发而不是第二次?如果是这样,则某些逻辑不会在 Page.IsPostback = true 状态下重新绑定中继器,另请注意,您不必对 ShopCartItemIdHiddenField 使用隐藏字段,有一个名为 CommandArgument 的内置属性可以对其进行数据绑定。
0赞 Satpal Singh 12/21/2016
您现在能看一下吗?如果你能找到一些东西。

答: 暂无答案