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
You haven’t shown in your markup whether you’re adding ID as a datakey to the GridView.
And I think you might have a problem with the way you’re retrieving the ID in the delete logic: