簡體   English   中英

Onclick 事件處理程序之前的 OnClientClick 自定義消息框不起作用

[英]OnClientClick custom message box before Onclick event handler not working

我想要一個自定義消息框 OnClientClick。 如果用戶選擇是,則 C# 代碼中的 Onclick 事件處理程序應該得到觸發。 但不知何故,我無法使用 ASP.net 和 jquery 來做到這一點。

截至目前正在發生的事情

  • 僅觸發 C# 代碼

我所期待的

  • 客戶端確認消息(如果用戶單擊“是”)然后服務器端代碼觸發。

我的 HTML

 <form id="form1" runat="server">                                                                
     <div>
       <asp:Button ID="btnSubmit" Text="submit" runat="server" OnClick="btnDelete_Click" 
       OnClientClick="if(!ShowDeleteFormConfirmation()) {return false;};" />       
    </div>
</form>

Jquery

  function ShowDeleteFormConfirmation() {
        var confirmationMessage,
            dlgButtons = {
                "No": function () {
                    $(this).dialog("close");
                    return false;
                },
                "Yes": function () {
                    $(this).dialog("close");                        
                    return true;
                }
            };
        confirmationMessage = "This form has already been assigned and will be marked as deleted.";
        var $panelContainer = $("<div>" + confirmationMessage + "</div>").appendTo('body');
        $panelContainer.attr("title", "Confirmation to delete a form");
        var myPos = [$(window).width() / 2 - 100, 50];
        $panelContainer.dialog({
            modal: false,
            draggable: false,
            position: myPos,
            button: dlgButtons
        });
    }  

C# ////點擊

      protected void btnDelete_Click(object sender, EventArgs e)
             {
                 ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Delete button clicked.');", true);
             } ```

這就是我想要實現的。 http://jsfiddle.net/y5z01nbr/

謝謝你看。

好的,雖然如果您在 js 代碼中使用“確認”,您的代碼將起作用,那是因為 alert() 和 confirm() 暫停了調用代碼。

然而,今天,幾乎所有 WEB 代碼庫都被編寫為不會停止,也不會凍結瀏覽器。 jQuery 就是這樣一種系統。 (它不會暫停代碼)。 雖然我可以介紹等待的概念——這對於這篇文章來說有點太復雜了。

那么,這意味着什么:

the jQuery code does NOT halt, 
and thus when you click on the button, 
the client side code runs WITHOUT halting 
and thus the button click (server side code) will 
ALSO run right away - not waiting.

那么,在 jQuery 對話框的情況下呢? 你不能暫停代碼。 這意味着你必須把它倒過來。 將顯示 jQuery 對話框,然后您必須調用/運行/單擊該服務器端按鈕。 因此,您必須添加一個新按鈕,並在現有按鈕上使用 style="display:none"。 然后顯示對話框,並根據您的答案然后單擊(呼叫)您現在擁有/擁有的那個原始按鈕。

因此,代碼將如下所示:

<div>
   <asp:Button ID="btnSubmit" Text="submit" runat="server" OnClick="btnDelete_Click" 
   Style="display:none" clientIDmode="static" />     

   <asp:Button ID="btnSubmitX" Text="submit" runat="server"  
   clientIDmode="static" OnClientClick="ShowDeleteFormConfirmation();" />     

<\div>

所以我放了另一個按鈕——代碼后面沒有服務器。 隱藏第一個按鈕,不顯示,並刪除客戶端單擊。 我將客戶端點擊移動到第二個按鈕。

現在,我們可以這樣做:

     dlgButtons = {
            "No": function () {
                $(this).dialog("close");
            },
            "Yes": function () {
                $(this).dialog("close");                        
                $('#btnSubmit').click();
            }
        };

所以我們要做的是啟動對話框。 基於是,然后我們點擊我們的按鈕。 如果您選擇否,則關閉對話框,但無需執行其他操作。

一個一般的硬性規定? 您的瀏覽器代碼現在很少見阻塞代碼 - 調用 jQuery.ui 對話框,實際上大多數這些較新的 UI 控件? 代碼不會等待,不會停止。 這意味着您不能使用返回 true/false 來控制服務器端事件存根是否會運行(您可以使用 js confirm(),但不能使用 jQuery,因為它不會等待,也不會停止代碼) .

暫無
暫無

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

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