[英]Stop ImageButton OnCommand postback using javascript in asp.net 4.0 (Internet Explorer)
看來Internet Explorer惡魔再次襲擊了我,但我似乎無法弄清楚這一點:
我有一個使用OnCommand從我的數據庫中刪除特定條目的ImageButton。 但是,我已經實現了它,以便用戶首先確認他們要刪除特定的項目。
這是我的ImageButton的代碼
<asp:ImageButton runat="server" ID="imgDelete" ImageUrl="~/Controls/TaskDetails/Images/delete.png" OnCommand="Tag_DeleteCommand" Visible='<%# CanDelete %>' OnClientClick="javascript:confirmDialog(this, 'Remove Tag','Are you sure you want to remove this tag?', 1); return false;"
CommandArgument='<%# Eval("TagID") %>' />
這是我的ConfirmDialog函數
function confirmDialog(sender, title, message, type) {
//type entities
//1 = warning
//2 = error
//3 = success
//4 = Information
var sender = $(sender);
$("#message_dialog_inner_text").html(message);
$("#message_dialog_message_container").dialog({
modal: true,
title: title,
zIndex: 10003,
dialogClass: (type > 0) ? "message_dialog_type_" + type : "",
position: 'center',
buttons: {
"OK": function () {
//the yes option was selected, we now disable the onclientclick event, and fire the click event.
$(this).dialog("close");
if (sender.hasAttr("href")) {
window.location = sender.attr('href');
} else {
sender.removeAttr("onclick");
sender.trigger("click");
}
},
"Cancel": function () { $(this).dialog("close"); }
}
});
return false;
}
這在Chrome,FF,Safari中可以正常工作,但IE只是忽略它並進行回發。 我什至將我的OnClientClick更改為“ return false”; 似乎對回發按鈕沒有影響。 我也將其從ImageButton更改為只是一個Button,無濟於事。 我假設它與OnCommand事件有關,但是對我來說很茫然! 我已經實現了這個非常龐大的應用程序,因此我想管理更改的范圍。 如果有人提出意見,將不勝感激!
編輯
好吧,我也要強調一點,我不希望它回發,即使我只有OnClientClick =“ return false;” 命令按鈕仍在回發(不是您期望的那樣)。
這是我的ImageButton控件的渲染標記
<input type="image" name="ctl00$body$pnlContentRight$pnlCurrentTaskDetails$repTags$ctl00$tagItem$imgDelete" id="ctl00_body_pnlContentRight_pnlCurrentTaskDetails_repTags_ctl00_tagItem_imgDelete" src="Controls/TaskDetails/Images/delete.png" onclick="javascript:confirmDialog(this, 'Remove Tag','Are you sure you want to remove this tag?', 1); return false;" style="border-width:0px;">
這不是解決您的特定問題的方法,但是您可能要考慮一個變通方法(我已經在幾個地方使用過):
從ASP.NET純粹主義者的角度來看,這可能不是實現此行為的“正確”方法。 但是,隨着變通辦法的發展,我認為這不是很痛苦的事情。 以我的經驗,它似乎可以在所有瀏覽器上正常工作(當然,假設它們啟用了JS)。 HTH。
您是OnClientClick
始終返回false,這意味着該事件將永遠不會執行。 嘗試像這樣從確認返回輸入:
OnClientClick="return confirmDialog(...);"
您將需要確保也有一種方法可以從confirmDialog
函數返回true。 看起來該函數也總是返回false。
我對jQuery UI對話框不是很熟悉,但是在概念上應該像這樣:
buttons: {
"OK": function () { return true; }, //perform a postback and execute command
"Cancel": function () { return false; } //abort postback and do nothing
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.