How to call confirm message from code behind in asp.net?

Hi I want to call a client side javascript confirm message from code behind in asp.net.

I want to use the true or false return value from the confirm message.

I’m doing like this, but this is not the correct way, please tell me how can I do the same.

ScriptManager.RegisterStartupScript(this, this.GetType(), "myconfirm", "confirm('No Rule Type Ids found for This Rule.');", true);

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

I think This is what you want to achieve:

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

.aspx code

<asp:Button ID="btnConfirm" runat="server" OnClick = "OnConfirm" Text = "Raise Confirm" OnClientClick = "Confirm()"/>

C#

public void OnConfirm(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);
    }
}

Method 2

Instead of directly writing confirm in the code behind. Write the name of javascript function.
Forexample,

ScriptManager.RegisterStartupScript(this, this.GetType(), "myconfirm", "OpenConfirmDialog();", true);

In your javascript, write the function OpenConfirmDialog

<script>
function OpenConfirmDialog()
{
    if (confirm('No Rule Type Ids found for This Rule.'))
    {
       //True .. do something
    }
    else
    {
       //False .. do something
    }
}

</script>

Method 3

You can’t mix client-side code with server-side code. The client side code (javascript) will not be sent to the client (browser) until the server-side code has completed.

You will need to stop processing and show the question to the user (maybe by a redirect to some other page), and then (on confirm) continue with your processing.

Method 4

Response.Write("<script language=javascript>");
                        Response.Write("if(confirm('El vehiculo no existe, Deseas crear el registro?')){window.location.href='IngresoVehiculos.aspx'}");
                        Response.Write("</script>");

Method 5

use:

Response.Write("<script>alert('Open Message!');</script>");

Method 6

You can do it without using Javascript function

Try

if (MessageBox.Show("confirm('No Rule Type Ids found for This Rule.')",
            "myconfirm",
            MessageBoxButtons.YesNo) == DialogResult.Yes)
    {
        // yes
    }
    else
    {
        //No
    }


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

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x