Janwel
Janwel

Reputation: 300

GridView ImageButton confirm and delete record

I've got a huge problem here. I've managed to add javascript to my server side, but the problem it is not deleting. It doesnt have produce any errors so I don't 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

Upvotes: 4

Views: 2839

Answers (2)

James Johnson
James Johnson

Reputation: 46047

You haven't shown in your markup whether you're adding ID as a datakey to the GridView.

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

And I think you might have a problem with the way you're retrieving the ID in the delete logic:

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

Upvotes: 1

Andrei
Andrei

Reputation: 56688

When the ImageDelete button is pressed, RowCommand event occurs. However, in the event handler for RowCommand you are not deleting anything. On the other hand RowDeleting event won't occur at all, since the correct command for this event is Delete, not cmdDelete.

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

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 your already implemented RowDeleting handler:

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

Upvotes: 2

Related Questions