Solved

confirm message box before deleting

Posted on 2004-10-01
3
24,730 Views
Last Modified: 2011-08-18
When clicking on a button (BtnDelete) on my mypage.aspx, I want to show a messagebox to confirme before deleting. I tried with the code below but nothing happens and the information is deleted from the database without any message box appearing. can you add the appropriate code to the code below so that it will work. Thanks.

 Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click

        'Shows a confirmation box when button delete is clicked
        BtnDelete.Attributes.Add("onclick", "return confirm('are you sure you want to delete? ');")

        'Declare objects and create a connection
        Dim myConnection As OdbcConnection = New OdbcConnection(ConfigurationSettings.AppSettings("connectionString"))
        Dim CmdString As String = "Delete From Kunder Where (ordernr = ? )"

        Dim myCommand As New OdbcCommand

        Try
            'Get parameter values from admin_kund.aspx
            Dim strOrdernr As String
            strOrdernr = Request.QueryString("ID")
            'Add parameters
            myCommand.Parameters.Add("ordernr", CStr(strOrdernr))

            myConnection.Open()

            myCommand.Connection = myConnection
            myCommand.CommandText = CmdString
            myCommand.ExecuteNonQuery()
            BindData()
        Catch ex As Exception
                myConnection.Close()
            End If
        End Try
    End Sub
0
Comment
Question by:viktor999
3 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 50 total points
ID: 12198535
Hi viktor999,

Way to late to set the attribute! If you set it in the event handler for the button itself it has already been clicked and the form has already been posted back. You should set the onlick attribute in the page load event handler as you want it to apply to the button as it is rendered to the page initially.

Tim Cottee
Brainbench MVP for Visual Basic
http://www.brainbench.com
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 12199188
You need this line in Page_Load event :

------------------------------------------------------------------------------------------------------------------------

        'Shows a confirmation box when button delete is clicked
        BtnDelete.Attributes.Add("onclick", "return confirm('are you sure you want to delete? ');")

------------------------------------------------------------------------------------------------------------------------

-tushar
0
 
LVL 10

Expert Comment

by:jnhorst
ID: 12202815
Even if you put the confirm() function in the onclick attribute in Page_Load, if you were to say No to the delete, the server-side code will still fire unless you explicitly cancel the event.  But there is a way to handle this strictly server-side:

1) Put a label control beneath your button but sets its Visible property to false.  Name it lblConfirm.
2) Set the CommandArgument property of your button to "Confirm" (u can use any string, but "Confirm" will do for this example).
3) In your server-side click event, do this:

Dim btn As Button = CType(sender, Button)
If btn.CommandArgument = "Complete" Then
     lblConfirm.Text = "Click Delete button again to confirm delete."
     lblConfirm.Visible = True
     btn.CommandArgument = "Delete"

Else If btn.CommandArgument = "Delete"
     ' execute your delete code here.

     lblConfirm.Text = ""
     lblConfirm.Visible = False
     btn.CommandArgument = "Confirm"

End If

You are using the label as a message box of sorts and the CommandArgument of the button to tell the click event whether you need to confrm or have confirmed.

John
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
creating a flowchart from an algorithm 5 30
Run time Error 4 34
IIS issues - Convert to Application - APS.NET v4.0 3 23
Need a starter for ETL protocol? 4 43
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

21 Experts available now in Live!

Get 1:1 Help Now