locked
Getting a Confirmation Before Gridview Row Delete - Overthinking This RRS feed

  • Question

  • I really hope I'm overthinking this, or just missing the obvious.  I have a gridview and am trying to use it's built-in delete functionality.  I had everything working great -- I'm using the gridivew RowDeleting method, and had a MessageBox prompting the user for confirmation.  This worked perfectly until I moved it out onto our production server, and now it fails.  Googling for why, it was explained that you can't use MessageBoxes in a client-server environment and it was suggested to use a javascript confirm method instead.  I've been working on this the entire day, but the problem with the javascript confirm method is that after it gets called, my code continues to run without waiting for the user response.

    I'm thinking, there has to be a much simpler way to get a confirmation box, isn't there? 

                                <asp:GridView ID="gvInterventionOptions" runat="server" AllowPaging="True" AllowSorting="True"
                                    AutoGenerateColumns="False" DataSourceID="SqlInterventionCodes" DataKeyNames="interventionID,interventionTypeID"
                                    PageSize="30" CssClass="gv" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" ShowFooter="True">
    
        Protected Sub gvEnrichmentCodes_RowDeleting(sender As Object, e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvEnrichmentCodes.RowDeleting
            Dim row As GridViewRow = gvEnrichmentCodes.Rows(e.RowIndex)
            Dim desc As String = CType(row.FindControl("Label3"), Label).Text
    
            'NOTE: This is where I initially had the MessageBox prompt...this next line is now calling a method that sets up the javascript confirm function
            Dim del As String = MsgBox("Deleting the enrichment code " + desc + " will also remove it as an Enrichment Option for any Students who have selected it." + vbCrLf + vbCrLf + "Do you still wish to delete it?", MsgBoxStyle.YesNo, "PLEASE CONFIRM!!")
    
            If del = "1" Then
                SqlEnrichmentCodes.DeleteParameters("schoolID").DefaultValue = Session("SchoolID")
                SqlEnrichmentCodes.DeleteParameters("original_enrichmentID").DefaultValue = gvEnrichmentCodes.DataKeys(e.RowIndex).Values("enrichmentID")
    
                gvStudentEnrichmentChoices.DataBind()
            Else
                e.Cancel = True
            End If
        End Sub
    


    Thursday, February 21, 2013 10:29 PM

Answers

  • Hi

    Try these changes (I dont have all your code so this is just typed blind)

            Dim del As DialogResult = MessageBox.Show("Deleting the enrichment code " & desc & " will also remove it as an Enrichment Option for any Students who have selected it." & vbCrLf & vbCrLf & "Do you still wish to delete it?", "PLEASE CONFIRM!!", MessageBoxButtons.YesNo)
    
            If del = Windows.Forms.DialogResult.Yes Then
                SqlEnrichmentCodes.DeleteParameters("schoolID").DefaultValue = Session("SchoolID")
                SqlEnrichmentCodes.DeleteParameters("original_enrichmentID").DefaultValue = gvEnrichmentCodes.DataKeys(e.RowIndex).Values("enrichmentID")
    
                gvStudentEnrichmentChoices.DataBind()
            Else
                e.Cancel = True
            End If
    


    Regards Les, Livingston, Scotland

    • Proposed as answer by Mike Feng Friday, February 22, 2013 9:05 AM
    • Marked as answer by puffster Friday, February 22, 2013 4:09 PM
    Thursday, February 21, 2013 10:44 PM

All replies

  • Hi

    Try these changes (I dont have all your code so this is just typed blind)

            Dim del As DialogResult = MessageBox.Show("Deleting the enrichment code " & desc & " will also remove it as an Enrichment Option for any Students who have selected it." & vbCrLf & vbCrLf & "Do you still wish to delete it?", "PLEASE CONFIRM!!", MessageBoxButtons.YesNo)
    
            If del = Windows.Forms.DialogResult.Yes Then
                SqlEnrichmentCodes.DeleteParameters("schoolID").DefaultValue = Session("SchoolID")
                SqlEnrichmentCodes.DeleteParameters("original_enrichmentID").DefaultValue = gvEnrichmentCodes.DataKeys(e.RowIndex).Values("enrichmentID")
    
                gvStudentEnrichmentChoices.DataBind()
            Else
                e.Cancel = True
            End If
    


    Regards Les, Livingston, Scotland

    • Proposed as answer by Mike Feng Friday, February 22, 2013 9:05 AM
    • Marked as answer by puffster Friday, February 22, 2013 4:09 PM
    Thursday, February 21, 2013 10:44 PM
  • I think what you have would work if I spent more time with it.  I copied your code over, and also had to Import System.Windows.Forms.  When I did this, other pieces of my code where I was using FindControl started complaining that FindControl was not a member of the Forms class (I never said it was!! :) )....anyway I did one more quick google search, and found the article below, which shows how to use the Confirm javascript box, and this works really well.

    Thanks, though, for taking the time to answer and help out a peer!!

    http://www.aspdotnet-suresh.com/2011/01/how-to-delete-records-in-gridview-with.html?showComment=1361549245880#c3799277104673582920

    Friday, February 22, 2013 4:11 PM