在asp.net 4.0 (Internet Explorer)中使用javascript停止ImageButton O
Stop ImageButton OnCommand postback using javascript in asp.net 4.0 (Internet Explorer)
看起来Internet Explorer的恶魔又来了,而我似乎无法弄清楚这个:
我有一个ImageButton使用OnCommand从我的数据库中删除一个特定的条目。然而,我已经实现了它,以便用户首先确认他们想要删除一个特定的项目。
下面是我的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为"返回false;",它似乎没有影响按钮张贴回来。我还将它从ImageButton更改为按钮,但无济于事。我假设它与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;">
这不是针对您的特定问题的解决方案,但这是您可能想要考虑的解决方案(我已经在几个地方使用了它):
- 让你的"删除"按钮变成一个简单的图片,而不是一个回复按钮。
- 给它一个调用本地javascript函数的OnClientClick(传递相关数据-至少,要删除的项目的ID)。 本地JS函数将相关数据加载到一个隐藏的变量中(因此它可以在你的代码后面访问),并打开jQuery确认对话框。
- 该对话框配置没有jquery-dialog按钮。相反,启动的DIV包含两个asp:Buttons: ok和cancel。取消按钮只是关闭jQuery对话框。OK按钮是一个普通的回发按钮,具有服务器端的OnClick功能。
- 因此,只有当用户单击确认对话框上的OK按钮时,页面才会被回发。confirm-dialog-event函数知道要删除哪个项,因为该项的ID在隐藏字段中。
这可能不是实现此行为的"正确"方式。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
}
- 如果弹出窗口关闭,Javascript停止/重置计时器
- 使用confirm从按钮中的javascript停止angular
- 如何使用 javascript 停止对 asp.net 进行正在进行的回发(长时间运行的执行)
- Javascript停止函数错误
- 使用JavaScript停止所有HTML5音频/视频
- 导致javascript停止运行的最大高度
- wordpress 4.1 javascript停止工作
- 宣布获胜者后,Tic-tac-Toe javascript停止切换回合
- javascript停止一个无限循环
- Firefox Javascript停止错误
- 我可以使用javascript停止元刷新吗
- 包含 jQuery 会导致标准 JavaScript 停止运行
- JSF richfaces 会导致 JavaScript 停止工作
- 用javascript停止html声音
- JavaScript 停止在 Firefox 和 Chrome 中的自动登录
- 多功能 javascript 停止其他如果警报取消
- Javascript 停止自动舍入
- 通过纯javascript停止并再次运行计时器
- 当我更改所需的输出时,Javascript 停止工作
- 通过 JavaScript 停止 C# 中的事件