2016-03-03 44 views
2

事件防火規範我的ASP.NET Web窗體應用程序ASP Web窗體後面的條件

<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警告問OK或Cancel.I希望如果選擇OK來觸發CODE BEHIND方法調用DeleteDealsButton_Click,如果取消則不做任何事情。

回答

1

剛更改按鈕定義爲,

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

不需要任何javascript函數。

+0

但是在用戶選擇好之後,我希望代碼繼續在事件調用DeleteDealsButton_Click後面進行編碼! –

+0

是的。如果用戶點擊OK按鈕,它將執行按鈕的點擊事件。如果用戶點擊取消按鈕,它將取消事件。 –

+0

這是作品10x –

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")) 
    { 
    } 
} 
+0

不工作我在NullReferenceException上有confirmValue.Equals(「是」) –