On my gridview I associate a javascript OnClick method to a linkbuton that asks to confirm the deletion of the bounded row:
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>
<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. Unfortunately when clicked Ok nothing happens and the ASP.net method is never triggered. 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.
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 :)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.