0

I need to add a confirm delete action to a grid. the problem is the way the "Delete" link is rendered. my grid is built in code behind in vb.net. i have this

colDelete.AllowDelete = True
colDelete.Width = "100"
AddHandler CType(gridSavedForLater, Grid).DeleteCommand, AddressOf dgDeleteSelectedIncident

and the sub is the following

Sub dgDeleteSelectedIncident(ByVal sender As Object, ByVal e As GridRecordEventArgs)

    Dim message As String = "Are you sure you want to delete this incident?"
    Dim caption As String = "Confirm Delete"
    Dim result = System.Windows.Forms.MessageBox.Show(message, caption, Windows.Forms.MessageBoxButtons.OKCancel, Windows.Forms.MessageBoxIcon.Warning)

    'If (result = Windows.Forms.DialogResult.Cancel) Then
    'Else
    ' MessageBox("Are you sure you want to delete this incident?")
    'Get the VehicleId of the row whose Delete button was clicked
    'Dim SelectedIncidentId As String = e.Record("IncidentId")

    ''Delete the record from the database
    'objIncident = New Incidents(SelectedIncidentId, Session("userFullName"))

    'objIncident.DeleteIncident()
    ''Rebind the DataGrid
    LoadSavedForLater()
    ''        End If

End Sub

i need to add a javascript confirm dialog when this sub is called. i can do it with a windows form messagebox but that does not work on the server.

pleae help joe

Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
Joe Cook
  • 71
  • 4
  • 16
  • You simply need to write a javascript function that handles the onclick event of the button and blocks/allows postback according to the prompt result. – MarioDS May 16 '12 at 14:42

2 Answers2

5

You cannot show a MessageBox in ASP.NET since it would be shown on the server. So you need a javascript confirm onclick of the delete button. Therefor you don't need to postback to the server first. You can attach the script on the initial load of the GridView.

A good place would be in RowDataBound of the GridView:

Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    Select Case e.Row.RowType
        Case DataControlRowType.DataRow
            ' if it's a custom button in a TemplateField '
            Dim BtnDelete = DirectCast(e.Row.FindControl("BtnDelete"), Button)
            BtnDelete.OnClientClick = "return confirm('Are you certain you want to delete?');"
            ' if it's an autogenerated delete-LinkButton: '
            Dim LnkBtnDelete As LinkButton = DirectCast(e.Row.Cells(0).Controls(0), LinkButton)
            LnkBtnDelete.OnClientClick = "return confirm('Are you certain you want to delete?');"            
    End Select
End Sub
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

As an alternative, a common method of notifying users in a web page of a status change (save, delete, update, etc.) is having an update element in your HTML. Basically, a label (or whatever) that you'll set with updates to the user.

"Your changes have been successfully saved." or "An error was encountered when trying to save your update.", for example. You can set the color red for an error or whatever your programming heart desires, stylistically.

I kind of like this approach as a pop-up always feels more like a WinForm thing to me. Either works, so I just thought I'd suggest another approach.

Yatrix
  • 13,361
  • 16
  • 48
  • 78