if (confi...">

How to call the Confirm method

I have a method JS

like this: (I don't know if it matches 100%)

<script Type="Text/Javascript">

if (confirm("Are you SUre ?")) {
  return true;
} else {
  return false;
}

</script>

      

And there are some doubts:

First: I have a folder named JS

and I plan to put it there, is that correct?

Second: I will be using this on the page Edit

, so when the client is click

on the button Edit

, I would like to receive this confirmation with some type message "Are you Sure?

and Yes / No buttons. If the client is clicks

on Yes

, then I will save mine edit on database

. So how do I go about doing this? How do I call this method the moment the client clicks on ImageButton

?

One more thing, how do I get the return of this method and work with it in my aspx.cs using C #?

I tried the code below but didn't work = \ it shows me popUp but even when I click cancel

it goes to the methodOnClick

<asp:ImageButton ID="Btn_Alterar" runat="server" ImageUrl="~/Imgs/btAlterar.jpg" OnClick="Btn_Alterar_Click" OnClientClick="confirm()" />

      

+3


source to share


2 answers


This should work. A question will appear and if they say yes, it will process the server side code (as an example).

JS:

<script language="javascript">
<!--
function doConfirm() {
    if (confirm("Are you SUre ?")) {
      //Person has said Yes, do your thing. Next line will allow the server side code to be processed
      return true
    } else {
      //User said No, do nothing and leave next line so the function call exits. 
      return false
    }
}
-->
</script>

      

ASP.NET Control:

<asp:ImageButton id="bMyButton" runat="server" onClientClick="doConfirm()" OnClick="DoServerSideCode" />

      

.cs



protected void DoServerSideCode(object sender, EventArgs e)
{
    //This will be called if JS is disabled OR the user clicks Yes. If the user clicks No then this won't be called. You can do DB logix here.
}

      

Edit: (thanks to @justnS)

Another implementation:

ASP.NET Control:

<asp:ImageButton id="bMyButton" runat="server" onClientClick="return confirm('Are you sure?')" OnClick="DoServerSideCode" />

      

The above is exactly the same as the previous implementation, but takes less code and is done inline.

+3


source


$ ('# MyLink'). Click (function (e) {e.preventDefault (); // Do you have something to delete in the database? Go here.}



Of course, you need jQuery ..

0


source







All Articles