GridView ImageButton confirms and deletes record

I have a huge problem here. I managed to add javascript to the server, but the problem has not been removed. It does not produce any errors, so I do not know where to start:

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand If e.CommandName = "cmdDelete" Then Dim ID As Integer = Convert.ToInt32(e.CommandArgument) End If End Sub Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then Dim l As ImageButton = DirectCast(e.Row.FindControl("ImageDelete"), ImageButton) l.Attributes.Add("onclick", "javascript:return " & "confirm('Are you sure you want to delete this record " & DataBinder.Eval(e.Row.DataItem, "ID") & "')") End If End Sub Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting Dim ID As Integer = CInt(GridView1.DataKeys(e.RowIndex).Value) 'dim ID as Integer con.Open() 'gridview1.rows(e.rowindex).cells(0) Dim cmd As New SqlCommand("delete from [tblUser] where [ID] =@ID ", con) cmd.Parameters.AddWithValue("@ID", ID) cmd.ExecuteNonQuery() con.Close() End Sub 

Client side

  <asp:TemplateField HeaderText="Delete"> <ItemTemplate> <asp:ImageButton ID="ImageDelete" ImageUrl="" runat="server" CommandName="cmdDelete" CommandArgument='<%# Eval("ID") %>' /> </ItemTemplate> </asp:TemplateField> </Columns 
+4
source share
2 answers

You did not indicate in your markup whether you are adding an ID as a data item in the GridView.

 <asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" ...> 

And I think you might have a problem with how you retrieve the identifier in the delete logic:

 Dim ID As Integer = Convert.ToInt32(GridView1.DataKeys(e.RowIndex)("ID")) 
+1
source

When the ImageDelete button is ImageDelete , the RowCommand event RowCommand . However, in the event handler for RowCommand you do not delete anything. On the other hand, the RowDeleting event RowDeleting not occur at all, since the correct command for this event is Delete , not cmdDelete .

I see two possible fixes for this. First move the code for the delete operation to the RowCommand event RowCommand :

 Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand If e.CommandName = "cmdDelete" Then Dim ID As Integer = Convert.ToInt32(e.CommandArgument) con.Open() Dim cmd As New SqlCommand("delete from [tblUser] where [ID] =@ID ", con) cmd.Parameters.AddWithValue("@ID", ID) cmd.ExecuteNonQuery() con.Close() End If End Sub 

Second, change the command name and use the already implemented RowDeleting handler:

 <asp:ImageButton ID="ImageDelete" ImageUrl="" runat="server" CommandName="Delete" CommandArgument='<%# Eval("ID") %>' /> 
+2
source