Confirm Dialog Box - VB.NET Web Application

Posted on 2005-05-03
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
    LVL 23

    Expert Comment

    by:Jens Fiederer
    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
    LVL 1

    Author Comment

    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


        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">
    LVL 1

    Author Comment

    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?

    LVL 1

    Author Comment

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

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now