用户确认消息框如何在ASP.Net中工作

How does user confirmation message box work in ASP.Net

本文关键字:ASP Net 工作 确认 消息 用户      更新时间:2023-09-26

客户端如何知道只有当确认框结果ok时才向服务器发送请求,并且如果取消则留在页面上?另外,这种机制在ASP中有什么不同吗?Net和ASP。净MVC吗?

我得到的回答似乎告诉我如何实现这个功能。我想知道当用户单击OK/取消内部发生的事情的内部工作。浏览器如何知道它必须继续服务器调用或关闭自己什么都不做?

您可以使用简单的confirm box

$("#callConfirm").on("click", function(e) {
    if(confirm('Are you sure'))
        alert('Yes');
    else
        alert('No');
});

JSFIDDLE

Try This

<script>
   function CallConfirm()
    {
    if(confirm('Are you sure'))
      //do your stuff
    else
     return false;
    }
<script />

和asp的按钮像这样写

<asp:Button id="Click" runat="server" onclientclick="return  CallConfirm();"  onclick="btn_Click"/>

你可以在asp.net中使用bootstrap,它将帮助你给你新的外观,并帮助你在许多其他的事情,看看这个http://getbootstrap.com/,你可以使用bootstrap确认框。

 <asp:button 
    id="Button1" runat="server" text="Button" xmlns:asp="#unknown">OnClientClick="return confirmation();" onclick="Button1_Click"/>
</asp:button>
        <script type="text/javascript">
        function confirmation() {
        if (confirm('are you sure you want to delete ?')) {
        return true;
        }else{
             return false;
                    }
                }
    </script> 

我在aspx页面为按钮编写了这段Jquery代码。

步骤:

  1. 添加一个按钮,你想有一个确认框,添加它的jquery和div来显示确认框

  2. 在Page_Load上注册button的脚本,并编写将此脚本绑定到button上的方法

  3. 也不要忘记按钮点击的服务器端方法或事件,这些将在确认框OK确认后继续。

  4. 如果取消被点击,什么都不会发生,div将被关闭。

    <script type="text/javascript">
    function FileItem(callBackFunction, title, content) {
            $("#File-confirm").html(content).dialog({
                autoOpen: true,
                modal: true,
                title: title,
                resizable: false,
                height: 140,
                close: function (event, ui) { $(this).dialog("destroy"); },
                buttons: {
                    'Ok': function () {
                        callBackFunction(); $(this).dialog("destroy");
                    },
                    'Cancel': function () {
                        $(this).dialog("destroy");
                    }
            });
        }
    }
    

其中SaveBtn是UI中的按钮:

<asp:Button ID="SaveBtn" runat="server" Text="File"  OnClick="SaveBtn_Click"/>
<div id="File-confirm" style="display: none">
</div>

后面的代码:

FileConfirmRequest(SaveBtn, "Confirm", "Are you sure you want to file the changes?");
// In the Page_Load, write the above code
//Use this method later on the page
protected void FileConfirmRequest(Button control, string title, string message)
{
  string postBackReference = Page.ClientScript.GetPostBackEventReference(control, String.Empty);
  string function = String.Format("javascript:FileItem(function() {{ {0} }}, '{1}', '{2}'); return false;", postBackReference, title, message);
  control.Attributes.Add("OnClick", function);
}

现在,按钮的点击:

protected void SaveBtn_Click(object sender, EventArgs e)
 {
   //Do what you want to after OK Click from the confirm box
 }