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.





4 Responses to "Confirmation On Delete Using ImageButton in GridView"

  1. Anonymous Says:

    голые малолетки на фото http://free-3x.com/ износилование малолеток видео онлайн free-3x.com/ малолетки берут в рот [url=http://free-3x.com/]free-3x.com[/url]

  2. Anonymous Says:

    good example thanks!

  3. Anonymous Says:

    [url=http://www.payloansonline.com]payday advance[/url]
    This is the best way to get all your health products online like green coffee, african mango, phen375 and others. Visit now

    [url=http://www.amazines.com/article_detail.cfm/5247759?articleid=5247759]Buy Oxyhives[/url]

  4. Anonymous Says:

    [url=http://garciniacambogiaselectz.weebly.com]
    garcinia cambogia fruit extract[/url] is the best paunchy on fire wrest nearby in market for the nonce a days. Bow to upto 10 kg in 1 month. garcinia cambogia select

Post a Comment