Confirmation box when button is clicked

Posted on 2005-04-12
Last Modified: 2008-02-01
I have a button:
Private Sub btnGetSupp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGetSupp.Click
End Sub

when the button is clicked, I want a confirmation message box to appear with
OK  and  CANCEL  on it.

When OK is clicked, I want to run the code, and cancel it when CANCEL is clicked.

What the best way to do this? thanks
Question by:MikeMCSD
    LVL 44

    Accepted Solution

    Hi MikeMCSD,

    In your form_load add:

    btnGetSupp.Attributes.Add("onclick","return confirm('Do you mean to do this?');")

    This displays the javascript confirm dialog and if cancel is clicked returns false, this will prevent the postback from occuring.

    Tim Cottee
    LVL 16

    Author Comment

    thanks Tim, but that did not work.

    I would like to have the code for this put inside the button's code.
    LVL 7

    Expert Comment


    try this

               Response.Write("<script language='JavaScript'>alert('You message here!');</script>")

    hope this helps.

    LVL 16

    Author Comment

    Sorry Tim, it did work . . I messed something up

    But, is there a way to put the code inside the button itself?
    LVL 2

    Assisted Solution

    I  have used the free DialogWindow tool from MetaBuilders for this purpose

    1) Download the Source
    2) Add a reference to the dll into your project (right click on References in your Solution Explorer)
    3) Right click in Toolbox, Add/Remove Items, add the controls
    4) Add a DialogInputBox, DialogMessageBox or DialogWindow object to your page (see for demos)
    5) Add DialogOpenButton or DialogOpenLink to your page, tie it to the one of the above objects that you added
    6) Add logic to the event for your window to read the return code and perform specific functions when the popup window is acted upon
    LVL 44

    Expert Comment


    Sorry for not getting back to you, at home now.

    No is the simple answer, there are of course more complex answers. As this is an control it only has a limited set of attributes that you can define in the <asp:Button ...> definition, client-side event handlers are unfortunately not available. However you can do a number of things to change this behaviour, either as shown add the attribute to the button dynamically or it is also possible to create your own button control which extends the standard one and has this and/or other attributes available. The other option is to use a standard html button control and set its runat="server" property. This has the effect of allowing you to handle its properties in codebehind in the same way as a .net control but also allows you to specify event handlers and other attributes that are not available to the .net control.

    I cannot really say what approach would be best for you, they all have advantages and disadvantages depending on a whole bunch of different factors.
    LVL 16

    Author Comment

    There is a small conflict though . . before I put in your code, I had this:

    btnGetSupp.Attributes.Add("onclick", "')")

    But when I put both of these in Page_Load:

    btnGetSupp.Attributes.Add("onclick", "return confirm(''Do you mean to do this?');")
    btnGetSupp.Attributes.Add("onclick", "')")

    the Orders.aspx page does not open.
    But having  Response.Redirect("Orders.aspx") in the button click did work.
    I would like to have it open in it's own window though.
    LVL 44

    Expert Comment


    Then you need to combine the two:

    btnGetSupp.Attributes.Add("onclick","if (confirm('Do you mean to do this?')) {'orders.aspx');} return false;")

    You can put as much javascript code into these attributes as you like (obviously when it gets complicated it is much better to create a function and call that from the button event)


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, 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, fr…

    737 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

    19 Experts available now in Live!

    Get 1:1 Help Now