Confirm Dialog Box - VB.NET Web Application

I have a web appilcation which I want to use a confirm dialog box in.  I basically want to confirm the deletion of an item from a datagrid.  Here is the code I use for an alert dialog box..

        Dim Message As String = "Test"
        Dim strScript As String = "<script language=javascript>"

        strScript += "alert(""" & Message & """);"
        strScript += "</script>"

        If (Not Page.IsStartupScriptRegistered("clientScript")) Then

            Page.RegisterStartupScript("clientScript", strScript)

        End If

I can change the box to a confirm dialog box (by replacing "alert" with "confirm"), but how do I get the returned value?

NeoTek
LVL 1
NeoTekAsked:
Who is Participating?
 
Jens FiedererTest Developer/ValidatorCommented:
Try

    Private Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated
        sOutput = e.Item.ItemType.ToString
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            CType(e.Item.FindControl("btnDelete"), Button).Attributes.Add("onclick", " return DoDrop()")
        End If
    End Sub

to attach the DoDrop() function to each delete button (which should have ID=btnDelete, as in
<asp:TemplateColumn>
    <ItemTemplate>
        <asp:Button runat="server" ID="btnDelete"  Text="Delete" CommandName="Delete"  CausesValidation="false">
         </asp:Button>
     </ItemTemplate>
</asp:TemplateColumn>
0
 
Jens FiedererTest Developer/ValidatorCommented:
Normally, you don't WANT the value (if you do, just write a script that places a value in a hidden server-side field based on the value returned from confirm)... you want processing to proceed only if the user confirms.

Essentially, you want to bind a function such as

function DropConfirm() {      
      event.returnValue = window.confirm("Are you sure you want to delete this ?")
}

to the onclick event of the Delete button (note that this one is Internet Explorer-specific...the Netscape way is to return true or false)

How that can be done in a DataGrid (using e.Item.Attributes.Add("onclick",...) in the ItemCreated event) is described in
    http://weblogs.asp.net/guys/articles/173646.aspx
0
 
NeoTekAuthor Commented:
Can you explain how to do that a bit more?  Basically I only want to know if the user wants to procede, not the value.  Also, I am using VB.NET.  The example uses C++.NET.

NeoTek
0
 
NeoTekAuthor Commented:
Nevermind.  It is working now...sort of.

Here is my code...

 Private Sub dgStatus_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgStatus.DeleteCommand

        Dim ID As String = e.Item.Cells(0).Text
        Dim delCommand As SqlCommand = New SqlCommand("DELETE FROM BusinessCards WHERE ID=@ID", connCards)

        connCards.Open()
        delCommand.Parameters.Add("@ID", ID)
        delCommand.ExecuteNonQuery()
        daCards.Fill(DsStatus)
        connCards.Close()

        dvStatus.Table = DsStatus.BusinessCards
        dgStatus.DataSource = dvStatus
        dgStatus.DataBind()

    End Sub

    Private Sub dgStatus_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgStatus.ItemDataBound

        Dim l As LinkButton
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            l = CType(e.Item.Cells(0).FindControl("Delete"), LinkButton)
            l.Attributes.Add("onclick", "return confirm('Are you sure you want to delete " & Trim(e.Item.Cells(1).Text) & "?');")
        End If

    End Sub

The confirmation dialog box is working fine.  The only problem I am having is that when I choose to delete an item, it isn't removed from the datagrid list until the page is refreshed by another method (edit button clicked, page index changed, etc).  Why is that?

NeoTek
0
 
NeoTekAuthor Commented:
I ended up removing it from the data view as well.  Thanks for your help...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.