Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing javascript confirm box in code behind C#

Tags:

asp.net

c#-4.0

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..

like image 864
Nagaraj Avatar asked Mar 28 '13 05:03

Nagaraj


People also ask

How can write confirm box in JavaScript?

You can create a JavaScript confirmation box that offers yes and no options by using the confirm() method. The confirm() method will display a dialog box with a custom message that you can specify as its argument.

How do you use confirm in JavaScript?

Javascript | Window confirm() Method The confirm() method is used to display a modal dialog with an optional message and two buttons, OK and Cancel. It returns true if the user clicks “OK”, and false otherwise. It prevents the user from accessing other parts of the page until the box is closed.

What does the confirm () method return?

Definition and Usage The confirm() method displays a dialog box with a message, an OK button, and a Cancel button. The confirm() method returns true if the user clicked "OK", otherwise false .


1 Answers

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

like image 115
Rahul Gokani Avatar answered Oct 03 '22 08:10

Rahul Gokani