簡體   English   中英

ASP.NET確認對話框第二次不起作用

[英]ASP.NET confirm dialog not working the second time

我有一個表格,其中有一個下拉列表和許多標簽。 當我從下拉列表中選擇一個選項時,它將從與該選項相關的數據庫中獲取數據,並將其填充到字段中。 然后我有一個刪除按鈕。 當我單擊此按鈕時,將從列表中刪除列表中的特定選定字段。 為此,在刪除之前,每當我單擊刪除按鈕時,它會要求彈出確認對話框,當我單擊確定時,它將刪除。 我為此使用以下代碼。

的JavaScript

<script type = "text/javascript">
    function Confirm() {
        if (Page_ClientValidate()) {
            var confirm_value = document.createElement("INPUT");
            confirm_value.type = "hidden";
            confirm_value.name = "confirm_value";
            if (confirm("You really want to delete the data?")) {
                confirm_value.value = "Yes";
            } else {
                confirm_value.value = "No";
            }
            document.forms[0].appendChild(confirm_value);
        }
    }
</script>

C#

protected void button_delete_Click(object sender, EventArgs e)
{
    string confirmValue = Request.Form["confirm_value"];
    if (confirmValue == "Yes")
    {
        // delete data from database
    }
}

ASP.NET

<asp:Button ID="button_delete" Visible="false" Text="Delete" runat="server" OnClientClick="Confirm()" OnClick="button_delete_Click" />

最初,刪除按鈕設置為visible=false ,只有在從列表中選擇一個項目后,該按鈕才可見。 這會影響回發后的確認對話框嗎? 它是第一次運行,在我手動重新加載頁面並嘗試運行之后。 但是,當我連續執行而不重新加載頁面時,它第二次將無法工作。 這是什么問題?

根據條件將Confirm更改為返回true或false值。

function Confirm() {

    var isFormValid = Page_ClientValidate();
    var isConfirmedByUser = confirm("You really want to delete the data?");

    //returns true when form is valid and user confirms action
    return (isFormValid  && isConfirmedByUser);               
}

並將OnClientClick更改為

<asp:Button ID="button_delete" Visible="true" Text="Delete" runat="server" OnClientClick="return Confirm();" OnClick="button_delete_Click" />

當Confirm()向OnClientClick返回錯誤值時,不應執行回發。

暫無
暫無

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

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