Confirm Dialog Box - VB.NET Web Application

Posted on 2005-05-03
Medium Priority
Last Modified: 2008-01-16
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?

Question by:NeoTek
  • 3
  • 2
LVL 23

Expert Comment

by:Jens Fiederer
ID: 13920621
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

Author Comment

ID: 13920735
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.

LVL 23

Accepted Solution

Jens Fiederer earned 500 total points
ID: 13921067

    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:Button runat="server" ID="btnDelete"  Text="Delete" CommandName="Delete"  CausesValidation="false">

Author Comment

ID: 13921078
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)

        delCommand.Parameters.Add("@ID", ID)

        dvStatus.Table = DsStatus.BusinessCards
        dgStatus.DataSource = dvStatus

    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?


Author Comment

ID: 13921303
I ended up removing it from the data view as well.  Thanks for your help...

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question