• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

Confirmation box when button is clicked

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
2 Solutions
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
MikeMCSDAuthor Commented:
thanks Tim, but that did not work.

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

try this

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

hope this helps.

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

MikeMCSDAuthor Commented:
Sorry Tim, it did work . . I messed something up

But, is there a way to put the code inside the button itself?
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 http://www.metabuilders.com/Tools/DialogWindow.aspx 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

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 asp.net 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.
MikeMCSDAuthor Commented:
There is a small conflict though . . before I put in your code, I had this:

btnGetSupp.Attributes.Add("onclick", "javascript:window.open(Orders.aspx')")

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", "javascript:window.open(Orders.aspx')")

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.

Then you need to combine the two:

btnGetSupp.Attributes.Add("onclick","if (confirm('Do you mean to do this?')) {window.open('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)


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now