简体   繁体   English

ASP.NET确认对话框第二次不起作用

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

I have a form where I have a dropdownlist and many labels. 我有一个表格,其中有一个下拉列表和许多标签。 When I select an option from dropdownlist it fetched data from database related to that option and populates them to field. 当我从下拉列表中选择一个选项时,它将从与该选项相关的数据库中获取数据,并将其填充到字段中。 Then I have a delete button. 然后我有一个删除按钮。 When I click on this that particular selected field from list will be deleted from database. 当我单击此按钮时,将从列表中删除列表中的特定选定字段。 For this before deleting whenever I click on delete button it ask for popup confirm dialog and when I click OK it will delete. 为此,在删除之前,每当我单击删除按钮时,它会要求弹出确认对话框,当我单击确定时,它将删除。 I am using the below code for this. 我为此使用以下代码。

JavaScript 的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# 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.NET

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

Initially the delete button is set to visible=false and only after selecting an item from list it becomes visible. 最初,删除按钮设置为visible=false ,只有在从列表中选择一个项目后,该按钮才可见。 Does this affect the confirm dialog after postback or something? 这会影响回发后的确认对话框吗? It is working the first time and after I reload the page manually and try it works. 它是第一次运行,在我手动重新加载页面并尝试运行之后。 But when I do it continuously without reloading page it wont work the second time. 但是,当我连续执行而不重新加载页面时,它第二次将无法工作。 What is the issue here? 这是什么问题?

Change the Confirm to return a true or false value depending on the condition. 根据条件将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);               
}

and change the OnClientClick to 并将OnClientClick更改为

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

when the Confirm() return a false value to OnClientClick it should not do a postback. 当Confirm()向OnClientClick返回错误值时,不应执行回发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM