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")
{
}
else
{
}
}
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.