簡體   English   中英

用戶確認消息框如何在ASP.Net中工作

[英]How does user confirmation message box work in ASP.Net

僅當確認框結果確定並取消后,客戶端才知道如何將請求發送到服務器? 此外,此機制在ASP.Net和ASP.Net MVC中是否有任何不同?

我得到的答復似乎告訴我如何實現功能。 我想知道用戶單擊確定/取消內部發生的情況的內部工作。 瀏覽器如何得知它必須繼續進行服務器調用或關閉自身而什么也不做呢?

您可以使用簡單的confirm

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

JSFIDDLE

我在按鈕的aspx頁面中編寫此Jquery代碼。

腳步:

  1. 添加您要具有確認框的按鈕,添加其jquery和div以顯示確認框。

  2. 在Page_Load上為按鈕注冊腳本,然后編寫將在按鈕上綁定此腳本的方法。

  3. 同樣不要忘記單擊按鈕的服務器端方法或事件,在確認框中單擊“確定”后,該方法或事件將繼續。

  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是用戶界面中的按鈕:

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

現在,按鈕的Onclick:

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

嘗試這個

<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中使用引導程序,它將幫助您煥然一新,並在許多其他方面有所幫助,請參閱此http://getbootstrap.com/ ,您可以使用引導程序確認框。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM