3

Hi I'm trying to learn more JavaScript AJAX. Basically I would like to have a popup Yes No on a delete button that would precede the actual C# event being fired. I could do this all in C# but I know doing it client side would be beneficial as it would reduce server load.

I'm not sure exactly how to go about this. Any ideas?

Rustam
  • 1,875
  • 2
  • 16
  • 33
mush_mouth_4life
  • 111
  • 1
  • 2
  • 8

3 Answers3

3

You can use the javascript function called confirm

onclick="return confirm ('Are you sure you want to delete this _____?');"

This text parameter is the text that will be shown in the modal with the yes and no.

The Yes returns true which allows the postback to continue, but the No returns false and will stop the postback.

EDIT:

As mentioned by XaiSoft and Cybernate, if you are using an ASP.NET button you can also use the OnClientClick property which will be translated to onclick by the server.

Community
  • 1
  • 1
Josh Mein
  • 28,107
  • 15
  • 76
  • 87
  • 1
    Beaware, w3schools has a lot of mistakes. – Xaisoft Jun 13 '11 at 19:12
  • @Xaisoft Only if you are using an ASP.NET button. I thought I would do the onclick because it works for both the ASP.NET button and a straight html input set as submit – Josh Mein Jun 13 '11 at 19:13
  • @Jmein & @Cybernate yeah I did something like this to get a popup but how does this then call the asp server side event ... If you use client side and server side on click event wont it fire both ??? – mush_mouth_4life Jun 13 '11 at 19:13
  • @user796466 The server side is not fired unless you hit the Yes. The No returns false which disables the postback. – Josh Mein Jun 13 '11 at 19:14
  • I tried `` But it fires the C# event regardless ... ??? – mush_mouth_4life Jun 13 '11 at 19:25
  • @user796466 Yeah that will be correct. I forgot to mention you also need a return before the confirm. I updated my answer to include it. I was trying to answer quickly and I forgot an important part :P. – Josh Mein Jun 13 '11 at 19:26
  • This works thankyou ... I will mark your's as answer even tho @cybernate gave a similiar answer but you kept getting back at me so ; ) ... also @zaisoft I'm sure this is a more elegant solution but its a little outside of my javascript knowledge at the moment thankyou tho – mush_mouth_4life Jun 13 '11 at 19:58
  • @user796466 Once you get going and understand javascript a little more, I would highly recommend learning jquery. There are many plugins that are available through jquery that will make your web interface more user friendly and will also make your life easier. – Josh Mein Jun 13 '11 at 20:03
2

You can use OnClientClick property of the asp:Button.. along with the JS confirm.. Try something like the code below in your aspx/ascx/master:

<asp:Button id="cmdSubmit" OnClientClick="return confirm('Are you sure?')" runat="server" .../>
Chandu
  • 81,493
  • 19
  • 133
  • 134
1

You can also use jQuery instead of the ugly default confirm. Here is a question addressing that.

Community
  • 1
  • 1
Xaisoft
  • 45,655
  • 87
  • 279
  • 432