Click here to Skip to main content
15,886,052 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Friends,

I have this ASP control:
XML
<asp:ImageButton id="ImageButton1" runat="server" ToolTip="Ver Facturas en Gracia. (Monto Bloqueado)" ImageUrl="../Comun/Recursos/images/icons/monto_bloqueado.png" </asp:ImageButton>


I need to call a confirm window (ask yes no cancel) in the c# code behind and next
process the response..

Please give me an example for this

Thanks in advace

Leonardo Ayala R.
Posted

C#
function ConfirmFunc(){
var retVal = confirm("Do you want to continue ?");
   if( retVal == true ){
      alert("User wants to continue!");
	  return true;
   }else{
      alert("User does not want to continue!");
	  return false;
   }
}


<asp:imagebutton id="ImageButton1" runat="server" tooltip="Ver Facturas en Gracia. (Monto Bloqueado)" imageurl="../Comun/Recursos/images/icons/monto_bloqueado.png" onclientclick="return ConfirmFunc();" xmlns:asp="#unknown"> </asp:imagebutton>
 
Share this answer
 
Comments
Aravindba 11-Mar-14 0:43am    
+5,in website try to use web controls only,don't mix windows controls or windows namespace
On the click event of button you can add this code to handle the YES/NO response.

C#
DialogResult result = MessageBox.Show("Do you want to exit?", "Confirmation", messageBoxButtons.YesNoCancel);
if(result == DialogResult.Yes){
    // do what you want!!
  }
else if (result == DialogResult.No){
    //do what you want!!
  }
else{
    //do what you want!!
  }
 
Share this answer
 
In this example we are going to ask the user if he/she really wants to clear the input on button click

First, add this script to your code behind

C#
<script type="text/javascript">
    function Confirm() {
        var confirm_value = document.createElement("INPUT");
        confirm_value.type = "hidden";
        confirm_value.name = "confirm_value";
        if (confirm("Are you sure you want to clear all your input?")) {
            confirm_value.value = "Yes";
        } else {
            confirm_value.value = "No";
        }
        document.forms[0].appendChild(confirm_value);
    }
    </script>
</script>


Second, on your button, add the Confirm() class

C#
<asp:button id="btnClear" runat="server" onclick="OnConfirm" onclientclick="Confirm()"

NOTE IT IS IMPORTANT TO SPECIFY THE onClick and onClientclick

Third and final step is right click your page and click view code and add this function:
C#
public void OnConfirm(object sender, EventArgs e)
       {
           string confirmValue = Request.Form["confirm_value"];
           if (confirmValue == "Yes")
           {
              //your function here

           }
           else
           {
              //your function here
           }
       }


That's it, you're good to go!

SIDENOTE: There is no cancel button, since basically it has the same functionality with no button.
 
Share this answer
 
v2
you can try it -<asp:imagebutton id="ImageButton3" runat="server" width="25" height="25" xmlns:asp="#unknown">
img src="../../AdminImages/delete.png" CommandName="Delete" ToolTip="Delete"
OnClientClick="return confirm('Are you sure want to delete record?')"
CausesValidation="False" />
 
Share this answer
 
 
Share this answer
 
v2
This confirm box help you to get correct response with having Ajax.


java script code for geting confirm value.

HTML
<script type="text/javascript">
        function Confirm() {
            var confirm_value = document.createElement("INPUT");
            confirm_value.type = "hidden";
            confirm_value.name = "confirm_value";
            if (confirm("Do you want to proceed continue ?")) {
                confirm_value.value = "Yes";
            } else {
                confirm_value.value = "No";
            }
            document.forms[0].appendChild(confirm_value);
        }
    </script>


On C# Code for geting exect data.
C#
string confirmValue = Request.Form["confirm_value"];
          string[] Value_Confirm = confirmValue.Split(',');
          if (Value_Confirm[Value_Confirm.Length-1] == "Yes")
          {}
 
Share this answer
 
v2
Comments
CHill60 9-Jan-16 10:07am    
Adds nothing to solutions posted over a year ago

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900