Implementing javascript confirm box in code behind C#
asp.net, c#-4.0
Solution
Can you try like this.:
<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"
OnClientClick = "Confirm()"
OnClick="OnConfirm"
Text="Raise Confirm"/>
</form>
</body>
</html>
Fetching the User input server side
Now server side we need to fetch the user input that we stored in the dynamic hidden field and then based on whether he has selected OK or Cancel we need to execute different code.
public void OnConfirm(object sender, EventArgs e)
{
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes")
{
//Your logic for OK button
}
else
{
//Your logic for cancel button
}
}
From Server Side (Code Behind) Yes No Confirmation Message Box in ASP.Net
Problem
I want to implement javascript confirm box from code behind. My requirement is in a method I need to raise a confirm box ,based on the result I need to implement diff functionalites For Example; if OK of Confirm Box the add Tax if cancel Then do not add tax I am trying something like this but its not helping me ``` ScriptManager.RegisterStartupScript(Page, Page.GetType(), "confirm", "confirm('Add tax');", true); ``` Can anyone help. my sample Code is ``` protected void Button1_Click(object sender, EventArgs e) { Double mrp = 200.00; ScriptManager.RegisterStartupScript(Page, Page.GetType(), "confirm", "return confirm('Add Tax');", true); if (Confirm == "Ok") { //Add tax to mrp } else { //No tax for mrp } } ``` Thank You..