简体   繁体   English

在gridview中的javascript确认后未触发onclick asp.net事件

[英]Onclick asp.net event not triggered after javascript confirmation in gridview

On my gridview I associate a javascript OnClick method to a linkbuton that asks to confirm the deletion of the bounded row: 在我的gridview上,我将一个javascript OnClick方法与一个linkbuton关联,该请求要求确认边界行的删除:

protected void DeleteRow_Click(object sender, EventArgs e)
    {
        GridViewRow grdrow = (GridViewRow)((LinkButton)sender).NamingContainer;
        using (ShoppingCartActions usersShoppingCart = new ShoppingCartActions())
        {
            string cartId = usersShoppingCart.GetCartId();

            int rowIndex = grdrow.RowIndex;

            //int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
            string itemId = ((HiddenField)CartList.Rows[rowIndex].FindControl("hiddenEncryptedItemID")).Value;
            usersShoppingCart.RemoveItemWithOptionsAndValues(cartId, itemId);
            CartList.DataBind();
            lblTotal.Text = String.Format("{0:N2}", usersShoppingCart.GetTotal());
            //return usersShoppingCart.GetCartItems();
        }
    }

For cosmetic reason I built my ItemTemplate this way: 出于装饰性原因,我通过以下方式构建了ItemTemplate:

<ItemTemplate>
   <div class="divItemTotal">
       <div>
           <asp:Label runat="server" CssClass="totalItem" ID="lblItemTotalPrice" Text='<%#: String.Format("{0:N2}", ((Convert.ToDouble(Item.Quantity)) *  Convert.ToDouble(Item.UnitPrice)))%>'></asp:Label>
            <asp:Label runat="server" ID="lblCurrency2" Text=" €"></asp:Label></div>
            <asp:LinkButton OnClick="DeleteRow_Click" CssClass="removeItem imgRemoveItem" runat="server" ID="DeleteRow"></asp:LinkButton>
       </div>

In an perfect world the javascript method should prevent the page to execute the postback and let it go if confirmed. 在理想情况下,javascript方法应阻止页面执行回发,并在确认后放手。 Unfortunately when clicked Ok nothing happens and the ASP.net method is never triggered. 不幸的是,当单击“确定”时,什么也没有发生,并且ASP.net方法从未触发。 Even more weird withtout the javascript function the asp method does only fire from time to time and I still have no assumption for that. 如果没有javascript函数,更奇怪的是asp方法只会不时触发,我对此仍然没有任何假设。

try this : 试试这个

<ItemTemplate>
  <div class="divItemTotal">
   <div>
      <asp:Label runat="server" CssClass="totalItem" ID="lblItemTotalPrice" Text='<%#: String.Format("{0:N2}", ((Convert.ToDouble(Item.Quantity)) *  Convert.ToDouble(Item.UnitPrice)))%>'></asp:Label>
      <asp:Label runat="server" ID="lblCurrency2" Text=" €"></asp:Label></div>
      <asp:LinkButton CommandArgument='<%# Eval("Item.Id") %>' CommandName="Delete" CssClass="removeItem imgRemoveItem" runat="server" ID="DeleteRow"></asp:LinkButton>
   </div> 

Code Behind: 背后的代码:

protected void CartList_RowDataBound(object sender, GridViewRowEventArgs e)
{ 
LinkButton l = (LinkButton)e.Row.FindControl("DeleteRow"); 
l.Attributes.Add("onclick", "javascript:return " +
"confirm('Are you sure you want to delete this record " +
DataBinder.Eval(e.Row.DataItem, "Item.Id") + "')");
}



protected void GridView1_RowCommand(object sender, 
                     GridViewCommandEventArgs e)
{
 if (e.CommandName == "Delete")
 {
   // get the categoryID of the clicked row
  int categoryID = Convert.ToInt32(e.CommandArgument);
  // Delete the record 
  DeleteRecordByID(categoryID);
// Implement this on your own :) 
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM