【问题标题】:How do you create a dialog message box using ASP .NET?如何使用 ASP .NET 创建对话框消息框?
【发布时间】:2012-10-04 22:05:40
【问题描述】:

在 WinForms 中,我可以这样做:

var msg = MessageBox.Show("Are you sure?", "WARNING!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);

if (msg == DialogResult.Yes)
{
    //do something
}

如何在 ASP .NET/C# 中模仿上述内容?

【问题讨论】:

  • 通常你不会。每当您想要一个弹出窗口时,您很可能只需要另一个常规页面。
  • 看看这个link,它不是你想要的,但它可以提供帮助。如果您想应用该代码,请告诉我帮助您。
  • 我们都可以使用一个好的对话框例程......我认为这是我多年前在 Clipper 中编写的第一个函数!

标签: c# asp.net dialog


【解决方案1】:

简短的回答是你不能。原因是你在服务器上执行的代码并没有直接映射到客户端。因此,您无法创建可在服务器上使用的交互式消息框来获取用户的响应。

您可以做的是使用客户端 jquery,然后根据他们的响应发出新请求。但是,这与在 WindowsForms 中使用 MessageBox 不同,因为它要求您让方法完全完成,然后根据全新的请求采取行动。

【讨论】:

    【解决方案2】:

    您可以随时使用JQuery Modal Dialogs:

    对话框定义:

    <div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-draggable ui-resizable">
       <div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix">
          <span id="ui-dialog-title-dialog" class="ui-dialog-title">Dialog title</span>
          <a class="ui-dialog-titlebar-close ui-corner-all" href="#"><span class="ui-icon ui-icon-closethick">close</span></a>
       </div>
       <div style="height: 200px; min-height: 109px; width: auto;" class="ui-dialog-content ui-widget-content" id="dialog">
          <p>Dialog content goes here.</p>
       </div>
    </div>
    

    对话框打开(只是一个例子):

    $(foo).dialog({ autoOpen: false })
    

    【讨论】:

    • 这无疑是显示对话框的最佳方式,因为 jQuery 对话框适用于所有浏览器,并且有很多自定义选项。
    • 这不符合提问者的要求。他想知道如何使用阻塞方法向用户发送请求,然后以相同的方法检查用户响应。这是做不到的。
    【解决方案3】:

    您不能从服务器端显示对话框。您可以使用 JavaScript 执行此操作。如果你想要它的样式,我推荐使用jQuery's dialog

    【讨论】:

      【解决方案4】:

      可以使用thisjquery插件

      【讨论】:

        【解决方案5】:

        如果您使用的是 asp:LinkBut​​ton

        <asp:LinkButton id="btnSubmit" runat="server" OnClick="btnSubmit_Click" />
        

        在后面的代码中:

        public void Page_Load(object sender, EventArgs e)
        {
            this.btnSubmit.Attributes.Add("onclick", "javascript: return confirm('Are you sure?');");
        }
        
        public void btnSubmit_Click(object sender, EventArgs e)
        {
            //Run code. User already confirmed to get here.
        }
        

        【讨论】:

        • 我喜欢这个...干净简洁。
        【解决方案6】:

        您可以使用 ASP.NET AJAX 来实现它

        http://www.asp.net/ajaxlibrary/act_ConfirmButton.ashx

        【讨论】:

          【解决方案7】:

          这是一个如何显示对话消息的基本示例:

          Page.RegisterStartupScript("UserMsg", "<script>alert('this is my message :) !');</script>");
          

          【讨论】:

            【解决方案8】:

            另一个解决方案是使用 Bootstraps 版本...我发现它很容易实现并且喜欢添加的效果。

            我有一个链接按钮

                <asp:LinkButton ID="btnDelete"
                      runat="server"
                      CssClass="btn btn-lg btn-primary"
                      Visible="true"
                      ToolTip="Delete Task"
                      data-toggle="modal"
                      data-target="#DeleteModal">
                      <span class="glyphicon glyphicon-trash"></span>
                </asp:LinkButton>
            

            还有删除模态分区

            <!-- Delete Modal Begin-->
            <div class="modal fade" id="DeleteModal" role="dialog">
                <div class="modal-dialog modal-sm">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal">&times;</button>
                            <h4 class="modal-title">Delete Task</h4>
                        </div>
                        <div class="modal-body">
                            <p>Are you sure you want to delete task?</p>
                        </div>
                        <div class="modal-footer">
                            <asp:Button ID="Button1" runat="server"
                                OnClick="btnDelete_Click"
                                CssClass="btn btn-primary"
                                Text="Delete" />
                            <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- Delete Modal End -->
            

            bootstrap modal example

            bootstrap modal reference

            【讨论】: