2014-03-25 33 views
0

只有當確認框結果正常時,客戶端如何知道向服務器發送請求,並且如果取消則停留在頁面上? 此外,這種機制在ASP.Net和ASP.Net MVC中有什麼不同?用戶確認消息框如何在ASP.Net中工作

我得到的迴應似乎告訴我如何實現該功能。 我想知道當用戶點擊確定/取消內部發生的內部工作。瀏覽器如何知道它必須繼續進行服務器調用或關閉本身而什麼也不做?

+0

您可以在此[文章]使用jquery作爲表演(http://stackoverflow.com/questions/4227043/how-do-i-cancel-form-submission-in-submit-button-onclick-事件) – Nilesh

回答

0

您可以使用簡單的confirm

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

JSFIDDLE

0

我寫在按鈕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 
} 
0

嘗試這個

<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"/> 
0

你可以在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>