【问题标题】:Dialog box wont appear不会出现对话框
【发布时间】:2020-08-26 14:27:30
【问题描述】:

当我注销时,我希望它显示一个你确定对话框,如果用户单击是,它将注销并清除缓存 - 但它永远不会出现,网站只是继续加载。谁能告诉我为什么?

   public void logout_click(object sender, EventArgs args)
    {


        var message = "Items in basket will be lost";
        var title = "Are you sure?";
        var result = MessageBox.Show(
            message,                  // the message to show
            title,                    // the title for the dialog box
            MessageBoxButtons.YesNo,  // show two buttons: Yes and No
            MessageBoxIcon.Question); // show a question mark icon


        if (result == DialogResult.Yes)
        {


            Session.Clear();
            Session.Abandon();
            Session.RemoveAll();
            FormsAuthentication.SignOut();
            Roles.DeleteCookie();



        }
    }

【问题讨论】:

  • 尝试只调用 messagebox.show ,这行得通吗?如果是这样,那么您的逻辑和 var 就是问题所在。
  • 是的,现在它可以工作了——尽管如果我点击否,我希望它取消。即使我单击否,ATM 也会注销。但是我应该在 if(result == dialogresult.no){ ??? 下写什么

标签: c# asp.net


【解决方案1】:

看起来您正在尝试在 ASP.NET 中使用 Windows.Forms MessageBox。它行不通。您必须在 Javascript 中实现确认。像这里的东西:How to show MessageBox on asp.net?

【讨论】:

    【解决方案2】:

    HTML MArkup

    HTML 标记由一个 ASP.Net 按钮 btnConfirm 组成。已为 Button 分配了 OnClick 和 OnClientClick 事件处理程序。

    当Button被点击时,OnClientClick事件会触发JavaScript Confirm方法。

    在 JavaScript Confirm 方法中,用户提供的输入存储在一个动态创建的隐藏字段中,即如果按下 OK,则存储值 Yes,如果按下 Cancel,则存储 No,以便我们可以将用户输入传递到服务器端代码。

    然后 Button 执行正常的 PostBack 并引发 OnClick 事件处理程序。

    <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" OnClick="OnConfirm" Text="Raise Confirm" OnClientClick="Confirm()"/>
        </form>
    </body>
    </html>
    

    在服务器端获取用户输入

    在 OnConfirm Click 事件处理程序中,获取存储在 Request.Form 集合的动态隐藏字段中的用户输入。

    然后根据用户是否选择了确定或取消,使用 JavaScript 警报消息框显示不同的消息。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多