简体   繁体   中英

How to make a confirm box?

I want to show a confirmation box from c# code rather than JavaScript. Is there any way I can have the confirmation box pop up when the below condition is true?

Here is the code so far:

if (items.SelectedNode.ChildNodes.Count >= 1)
{
    ScriptManager.RegisterStartupScript(this.nav_tree_items, typeof(string), "Alert", "alert('Hello');", true);
}

I have already tried add.attributes, but that does not work.

I also tried the following but on click of cancel it performs an action anyway:

if (items.SelectedNode.ChildNodes.Count >= 1)
{
    ScriptManager.RegisterStartupScript(this.nav_tree_items, typeof(string), "Confirm", "Confirm('Hello');", true);
}

Try this:

if (items.SelectedNode.ChildNodes.Count >= 1)
{
    ScriptManager.RegisterStartupScript(this.nav_tree_items, typeof(string), "Confirm", "return Confirm('Hello');", true);
}

get the return value of the Confirm function in order to cancel or proceed.

Update 1:

Or use ASP.NET Ajax Toolkit Confirm Button

You can use OnClientClick attribute of asp:Button .

Take the sample below:

<asp:Button ID="btnDelete" runat="server" OnClick="btnDelete_OnClick" OnClientClick="return confirm('Are you sure you want to delete time this record?');"/>

You can try adding confirm box, but you should take proper action after clicking on ok/cancel button.

if (items.SelectedNode.ChildNodes.Count >= 1)
   {
   ScriptManager.RegisterStartupScript(this.nav_tree_items, typeof(string), "Alert", 
      @"var conrimationFlag; conrimationFlag = confirm('Your confirmation 
      message goes here!'); 
      /* now, take proper action here based on the 
      value of variable conrimationFlag */ ", true);
   }

Thanks.

if (items.SelectedNode.ChildNodes.Count >= 1)
{
    Page.ClientScript.RegisterStartupScript(typeof(YourPage), "alert", "<script>alert('Hello')</script>");
}

Edit:

in page:

<script>
function ConfirmAlert()
{
   var result = confirm('Hello');
   if (result)
   {
      //click ok button
      //do something
   }
   else
   {
      //click cancel button
      //do something
   }
}
</script>

code behind

if (items.SelectedNode.ChildNodes.Count >= 1)
{
    Page.ClientScript.RegisterStartupScript(typeof(YourPage), "ConfirmAlert", "<script>ConfirmAlert()</script>");
}

look at this example

<script type="text/javascript">
<!--
function confirmation() {
    var answer = confirm("Do you want to exit...?")
    if (answer){
        alert("Bye bye!")
        window.location = "http://www.google.com/";
    }
    else{
        alert("Thank you very much Come Again!")
    }
}
//-->
</script>
</head>
<body>
<form>
<input type="button" onclick="confirmation()" value="Do you want to leaave">

Read about ScriptManager from http://msdn.microsoft.com/en-us/library/bb310408.aspx

Try it out,

ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "KEY", "alert('hello')", true);

try this one

if (items.SelectedNode.ChildNodes.Count >= 1)
{

String _scriptAuthor1 = "javascript:$(function () { " +
                        " return (confirm('Are you sure you want to delete ?')) });";
nav_tree_items.Attributes.Add("onClick", _scriptAuthor1);
}

instead of doing it with JavaScript do it with C#!...

DialogResult dialogResult = MessageBox.Show("Are you shure?", "Some Title", MessageBoxButtons.YesNo);

if(dialogResult == DialogResult.Yes)
{
//do something
}
else if (dialogResult == DialogResult.No)
{
//do something else
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM