Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am stuck over a very elementary code.
i want user to submit their message on webpage but i want a confirmation before message is saved. my source page is:
<head runat="server">
    <title></title>
    <script type = "text/javascript">
        function Confirm() {
            if (Page_ClientValidate()) {
                var confirm_value = document.createElement("INPUT");
                confirm_value.type = "hidden";
                confirm_value.name = "confirm_value";
                if (confirm("Do you want to save your message?")) {
                    confirm_value.value = "Yes";
                } else {
                    confirm_value.value = "No";
                }
                document.forms[0].appendChild(confirm_value);
            }
        }
</script>
</head>
<body>
    <form id="form1" runat="server">

 
    <div>
        <asp:TextBox ID="MessageTextBox" runat="server"></asp:TextBox>
        <asp:Button ID="btnConfirm" runat="server" OnClick="Button1_Click" Text="Raise Confirm" OnClientClick="Confirm()"/>
       
        
    </div>
    </form>
</body>

and my code behind is:
protected void Button1_Click(object sender, EventArgs e)
    {
        string confirmValue = Request.Form["confirm_value"];
        if (confirmValue == "Yes")
        {
            //code for saving the message in text box
        }
        else
        {
            //do nothing
        }

    }


When i click the button i do not get the confirmation box and nothing happens

What I have tried:

to identify the problem i used the following code behind
protected void Button1_Click(object sender, EventArgs e)
    {
        string confirmValue = Request.Form["confirm_value"];
        if (confirmValue == "Yes")
        {
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
        }
        else
        {
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
        }

    }

The result was that i did not get the confirmation box but i got the alert message "You clicked NO!"
the code appears to presume that i saw the confirmation box and clicked NO whereas i did not see the confirmation box at all.
Can anyone please advice what i am missing.
Posted
Comments
[no name] 30-Mar-21 14:04pm    
Don't "confirm"; add a checkbox as confirmation.
Member 10235977 30-Mar-21 16:20pm    
i dont know how well it will serves the purpose of alerting the user, definitely not as good as confirm. Talking of alternatives, i could also have used a PopupExtender in AjaxControlToolkit but it would sound like French princess saying 'If you don't get bread why don't you eat cake.
Anyway thanks for your help.
I have found solution myself anf for sake of people who keep making mistakes like me, the midstake i made was to add
if (Page_ClientValidate()) {
as a result the code acts with presumption of client validation.
I jjust removed this line from code on second thought and it worked.

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