【问题标题】:How to make a confirm box?如何制作确认框?
【发布时间】:2011-12-16 13:43:43
【问题描述】:

我想从 c# 代码而不是 JavaScript 显示一个确认框。 当以下条件为真时,有什么方法可以弹出确认框?

这是目前为止的代码:

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

我已经尝试过 add.attributes,但这不起作用。

我也尝试了以下操作,但单击取消后它仍然执行操作:

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

【问题讨论】:

  • 我必须有条件,才应该允许确认框。所以我更喜欢 c# 会比 JavaScript 更好

标签: c# asp.net


【解决方案1】:

试试这个:

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

获取 Confirm 函数的返回值,以便取消或继续。

更新 1:

或者使用 ASP.NET Ajax Toolkit Confirm Button

【讨论】:

    【解决方案2】:

    您可以尝试添加confirm 框,但您应该在点击ok/cancel 按钮后采取适当的措施。

    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);
       }
    

    谢谢。

    【讨论】:

    • 我说,基于conrimationFlag 的值需要采取更多行动,谢谢
    【解决方案3】:

    看看这个例子

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

    【讨论】:

    • 但如果父节点有子节点,我只想在条件为真时显示上述消息。
    • 然后更改警报中的消息(“Bye bye”)将其更改为您希望它显示的任何消息..用您需要的内容替换我的示例..
    【解决方案4】:
    if (items.SelectedNode.ChildNodes.Count >= 1)
    {
        Page.ClientScript.RegisterStartupScript(typeof(YourPage), "alert", "<script>alert('Hello')</script>");
    }
    

    编辑:

    在页面中:

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

    后面的代码

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

    【讨论】:

    • @user1047883 您可以在 Firefox 中使用 firebug 测试脚本。你可以看到错误的原因。
    【解决方案5】:

    http://msdn.microsoft.com/en-us/library/bb310408.aspx了解 ScriptManager

    试试看,

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

    【讨论】:

      【解决方案6】:

      试试这个

      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);
      }
      

      【讨论】:

      • 这并不要求我做任何事情。它只是执行操作。
      • 我将“on click”更改为“OnClientClick”
      【解决方案7】:

      您可以使用asp:ButtonOnClientClick 属性。

      以下示例:

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

      【讨论】:

        【解决方案8】:

        用 C# 代替 JavaScript!...

        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
        }
        

        【讨论】:

        • 为了将来参考,Messagebox.show() 不适用于 ASP.Net 网站,因为它仅适用于基于客户端的应用程序,如 Winforms 或 WPF 应用程序。只有 Javascript 可以在网页上显示警报。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-30
        • 1970-01-01
        • 1970-01-01
        • 2013-08-31
        • 1970-01-01
        • 2022-06-15
        • 2021-05-18
        相关资源
        最近更新 更多