ASP Web Forms事件背后的消防代码

时间:2016-03-03 09:33:22

标签: javascript jquery asp.net code-behind

我有ASP.NET WEB FORMS应用程序

<asp:Button ID="DeleteDealsButton" runat="server" Text="Delete" />

function btnDeleteClick() {
    var result = confirm("Want to delete all Deals?");
    if (result) {
        return true;
    } else {
        return false;
    }
}

$("#MainContent_DeleteDealsButton").on("click", function() {
        if (btnDeleteClick()) {})

在这个按钮上,我有一个示例javascript警报,询问确定或取消。我想如果选择确定以解雇CODE BEHIND方法调用DeleteDealsButton_Click,如果它取消什么都不做。

2 个答案:

答案 0 :(得分:1)

尝试这样:

function btnDeleteClick() {
var confirm_value = document.createElement("INPUT");
 confirm_value.type = "hidden";
 confirm_value.name = "confirm_value";
 confirm_value.value = "No";
var result = confirm("Want to delete all Deals?");
if (result) {
    confirm_value.value = "Yes";
    __doPostBack('DeleteDealsButton', '')
    return true;
} else {
    confirm_value.value = "No";
    return false;
}
}

在.Aspx文件中将按钮代码更改为:

<asp:Button ID="DeleteDealsButton" OnClientClick=" return btnDeleteClick()" runat="server" Text="Delete" OnClick="DeleteDealsButton_Click" />

在.Aspx.cs文件中

protected void DeleteDealsButton_Click(object sender, EventArgs e)
    {
     string confirmValue = Request.Form["confirm_value"];
     if (!String.IsNullOrWhiteSpace(confirmValue) && confirmValue.Equals("Yes"))
     {
     }
}

答案 1 :(得分:1)

只需将按钮定义更改为,

<asp:Button ID="DeleteDealsButton" runat="server" Text="Delete" OnClientClick="return confirm('Want to delete all Deals?')" />

不需要任何javascript功能。