-1

The below code runs a confirm messagebox with yes no on Asp.Net.
I need to detect the value if its confirmed or not.
How can I do this ?

Aspx :

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <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 save data?")) {
                confirm_value.value = "Yes";
            } else {
                confirm_value.value = "No";
            }
            document.forms[0].appendChild(confirm_value);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Button ID="btnConfirm" runat="server" OnClick = "OnConfirm" Text = "Raise Confirm" OnClientClick = "Confirm()"/>
    </form>
</body>
</html>

Code behind :

protected void OnConfirm(object sender, EventArgs e)
{
    // This method runs even though the user clicks no.   
}

Update :
With this code, both yes or no selections runs the same method named OnConfirm. So I try to run the OnConfirm method only if the user clicks yes.

Bengi Besçeli
  • 3,638
  • 12
  • 53
  • 87

2 Answers2

0

Update:

you can use a hidden field with runat=server and save yes/no. then send it to server.

 <input type="hidden" runat="server" value="" id="hidden1" />


function Confirm() {

        if (confirm("Do you want to save data?")) {
            confirm_value.value = "Yes";
            document.getElementById("hidden1").value = "yes";
        } else {
            confirm_value.value = "No";
            document.getElementById("hidden1").value = "no";
        }
        document.forms[0].appendChild(confirm_value);
    }

If you use a master page, remember that client id of hidden is different with its server id.

protected void OnConfirm(object sender, EventArgs e)
{
  string confirmValue = hidden1.Value;       
}
Farzin Kanzi
  • 3,380
  • 2
  • 21
  • 23
0

i think what you want to do is only execute the server side button when the user confirms OK right? if yes, then just do this, basically when the javascript function returns false, the server side button will not fire ie: (OnClientClick = "return Confirm()")

    <script type = "text/javascript">
        function Confirm() {           
            return confirm("Do you want to save data?");
        }
    </script>


    <asp:Button ID="btnConfirm" runat="server" OnClick = "OnConfirm" Text = "Raise Confirm" OnClientClick = "return Confirm()"/>
O. Gungor
  • 758
  • 5
  • 8