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..
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
Method 2
You can use ScrptManager like:
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "confirm", "return confirm('Your massage to be Displayed.');", true);
You can refer this link for more details.
Or you can try like this.
ScriptManager.RegisterStartupScript(this, typeof(string), "Message", "if(confirm('Sorry, but there is already a Primary Stakeholder assigned to this ticket. Would you like to replace the existing data?')){alert('OK');}else{alert('cancel');}",true);
In the adove code If user press OK button, It will display alert box with message OK else it will display alert box with message Cancel.
ScriptManager.RegisterClientScriptBlock(this, GetType(), "__Mensagem", "if (confirm('" + CustomMessage+ "')){$("#" + btnSave.ClientID + "").click();}", true);
Can you try this. In this if confirm returns true then it returns the click event of the button.
Method 3
please try following:
Double mrp = 200.00;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("var x=''; var r=confirm('Press a button!');");
sb.Append("if (r==true){alert('ok'); x='" + mrp + "';}");
sb.Append("else{ alert('cancel');}");
sb.Append("document.getElementById('demo').innerHTML=x;");
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "confirm", sb.ToString(), true);
aspx page:
<p id="demo"></p>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0