C# 如何使用 ASP .NET 创建对话框消息框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12736720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:17:02  来源:igfitidea点击:

How do you create a dialog message box using ASP .NET?

c#asp.netdialog

提问by RJ.

In WinForms, I can do this:

在 WinForms 中,我可以这样做:

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

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

How do I mimic the above in ASP .NET/C#?

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

回答by Erre Efe

You can always use JQuery Modal Dialogs:

您始终可以使用JQuery 模态对话框

Dialog definition:

对话框定义:

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

Dialog opening (just an example):

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

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

回答by lehn0058

You can't show a dialog from the server side. You would do this using JavaScript. If you want it styled, I recommend using jQuery's dialog

您无法从服务器端显示对话框。您可以使用 JavaScript 执行此操作。如果你想要它的样式,我建议使用jQuery 的对话框

回答by Erik Funkenbusch

The short answer is that you can't. The reason is that the code you execute on the server does not directly map to the client. So you can't create an interactive message box that you can use on the server to get responses from the user.

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

What you can do is use client-side jquery, and then issue new requests based on their responses. However, this is not the same as using a MessageBox in WindowsForms as it requires that you let the method finish completely, and then take action based on an entirely new request.

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

回答by giftcv

You can achieve it using ASP.NET AJAX

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

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

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

回答by Sirwan Afifi

you can use thisjquery plug-in

你可以使用这个jquery插件

回答by Lawrence Johnson

If you are using an asp:LinkButton

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

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

In code behind:

在后面的代码中:

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

回答by Jorgesys

This is a basic example of how to show a dialog message :

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

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

回答by Chris Catignani

Another solution is to use the Bootstraps version...I found it was pretty easy to implement and like the added effects.

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

I have a link button

我有一个链接按钮

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

And the Delete Modal Div

和删除模态 Div

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

引导模式参考