Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Very simple question. How do I get the value from confirm and use that value to do something?

For example...

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function validate()
        {
            return confirm('Are you sure to continue?');
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btn1" runat="server" Text="Press Here" OnClientClick="validate()" OnClick="btn1_Click" />
        <asp:Label ID="lb1" runat="server" />
    </div>
    </form>
</body>
</html>


With the code above I get the confirm message and I can choose "OK" or "CANCEL"

When I press OK, I want to display a message from Label.

If I press CANCEL, I want to display another message.

C#
public partial class JavascriptConfirm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btn1_Click(object sender, EventArgs e)
    {
        lb1.Text = "You Pressed OK";
    }
}


With the code above ofcourse it will display only "You Pressed OK" no matter what I choose from confirm message.

Can anyone help?
Posted

You can use the following code.

C#
var str="MyValue";
if(confirm("Click OK to Proceed")==true) //If OK then TRUE
{
    alert("Your OK message"+str)
}
else
{
    alert("Your Cancel message"+str)
}
 
Share this answer
 
Comments
drtiger 16-Oct-12 23:54pm    
Thank you for reply, but I want to know how to send the true/false value to code behind so I can do stuff like if(true) display some text, else display something else or do nothing.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
     function validate()
      {
        if(confirm('Are you sure to continue?'))        
             document.getElementById('hdnConfirmResult').value='true';         
        else         
             document.getElementById('hdnConfirmResult').value='false';        
       }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:hiddenfield id="hdnConfirmResult" runat="server" value="" xmlns:asp="#unknown" />
        <asp:Button ID="btn1" runat="server" Text="Press Here" OnClientClick="validate()" OnClick="btn1_Click" />
        <asp:Label ID="lb1" runat="server" />
    </div>
    </form>
</body>
</html>

Then in CodeBehind:
C#
protected void btn1_Click(object sender, EventArgs e)
 {
     bool Confirmed= Convert.ToBoolean(hdnConfirmResult.value);//can cause exception incase the value is not true or false
     if(bConfirmed)
        lb1.Text = "You Pressed OK";
     else
        lb1.Text="";//Whatever you want to display
 }

Although i dont understand why you want to postback to the server in case the user chose to cancel. If it is just to display this message then you are doing it wrong. you can display this message using javascript itself from clientside.
 
Share this answer
 
v2
Hi,

you can put another hidden button for the option No. say,

ASP.NET
<asp:Button id="btnNo" runat="server" onclick="btnNo_Click" style="visibility:hidden;" />


And in the js code, when user clicks on no in confirm dialog, you have trigger the no button's click and return false to cancel current button's click.

See the Code below:

C#
if(confirm("Click OK to Proceed")==true) //If OK then TRUE
{
        return true;
}
else
{
    document.getElementById('btnNo').click();
    return false;
}



And in code behind file you have to write click event handler for btnNo same way as btn1.

C#
protected void btnNo_Click(object sender, EventArgs e)
    {
        lb1.Text = "You Pressed Cancel";
    }



Hope this helps you :)
 
Share this answer
 
v2

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