Confirmation On Delete Using ImageButton in GridView

I was having a problem when it comes to confirm the user before delete the row using the imagebutton in gridview. One of the problem is the delete event is fire twice, meaning to say that the row is deleted twice. Here is my original source code of the gridview

Original Source Code in HTML code

<asp:GridView ID="gvProd" runat="server" SkinID ="GridView" AllowPaging="True" PageSize="5"
AutoGenerateColumns="False" Width="461px" DataKeyNames ="PRODUCT_ID" OnRowDataBound ="gvProd_RowDataBound" >
<Columns >
<asp:TemplateField >
<ItemTemplate >
<%#Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible ="False" ><ItemTemplate >
<asp:Label id="lblProdID" runat ="server" Text ='<%# DataBinder.Eval(Container, "DataItem.PRODUCT_ID") %>'></asp:Label>
</ItemTemplate></asp:TemplateField>
<asp:TemplateField Visible ="False" ><ItemTemplate >
<asp:Label id="lblProdCatID" runat ="server" Text ='<%# DataBinder.Eval(Container, "DataItem.PRODUCT_CATEGORY_ID") %>'></asp:Label>
</ItemTemplate></asp:TemplateField>
<asp:BoundField DataField="PRODUCT_NAME" HeaderText="Name"></asp:BoundField>
<asp:BoundField DataField="PRODUCT_CATEGORY_NAME" HeaderText="Category"></asp:BoundField>
<asp:BoundField DataField="FORMAT" HeaderText="Format"></asp:BoundField>
<asp:BoundField DataField="TYPE" HeaderText="Type"></asp:BoundField>
<asp:BoundField DataField="PRODUCT_PRICE" HeaderText="Price (RM)"></asp:BoundField>

<asp:TemplateField HeaderText ="Action" >
<ItemTemplate >
<asp:HyperLink runat="server" ID="hLink" Text ="Edit"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>

<asp:CommandField ShowDeleteButton="false" DeleteImageUrl ="~/images/delete.gif" />
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton runat=server id="imgDelete" CommandName="Delete"
ImageUrl="~/images/delete.gif" CommandArgument='<%# DataBinder.Eval(Container,"RowIndex") %>' OnCommand="ImageButton1_Command" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

(thanks to the online code formatter written by Jean-Claude Manoli) and lvildosola.blogspot.com for directing me to this formatter

After googling for the answer, I managed to solve the problem by removing the OnRowDeleting=”gvProd_RowDeleting” in the above code and adds the code for delete function in code behind as below

On RowBound

Protected Sub gvProd_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvProd.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
Dim LnkEdit As HyperLink = CType(e.Row.FindControl(“hLink”), HyperLink)
Dim lblProdID As Label = e.Row.FindControl(“lblProdID”)
Dim lblProdCatID As Label = e.Row.FindControl(“lblProdCatID”)
Cache(“lblProdID”) = lblProdID.Text
Cache(“lblProdCatID”) = lblProdCatID.Text
LnkEdit.NavigateUrl = “ProductManagements.aspx?action=editprod&ProdID=” & lblProdID.Text & “&CatID=” & lblProdCatID.Text
‘Delete button
Dim imgDel As ImageButton = CType(e.Row.FindControl(“imgDelete”), ImageButton)
imgDel.Attributes.Add(“onclick”, “javascript:return confirm (‘You are about to delete ” + DataBinder.Eval(e.Row.DataItem, “PRODUCT_NAME”) + “.’);”)
End If
End Sub

On ImageButtonCommand

Protected Sub ImageButton1_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
gvProd.DeleteRow(Int32.Parse(e.CommandArgument.ToString()))
End Sub

OnRowDeleting Command

Sub sbDeleteProds(ByVal ProdID)
Dim success As String = prodcat.fnDeleteProdByType(glb.Product, ProdID)
End Sub
Protected Sub gvProd_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvProd.RowDeleting
sbDeleteProds(Convert.ToInt32(gvProd.DataKeys(e.RowIndex).Values(0).ToString))
If Cache.Item(“Adv”) <> “” Then
BindData(“Adv”)
Else
BindData(“Basic”)
End If
End Sub

…..

and TAdaaaaa..finally it works for me and the row is deleted once.

Related Posts Plugin for WordPress, Blogger...
signature

Leave a Reply

Your email address will not be published. Required fields are marked *

CommentLuv badge