在asp.net 4.0 (Internet Explorer)中使用javascript停止ImageButton O

Stop ImageButton OnCommand postback using javascript in asp.net 4.0 (Internet Explorer)

本文关键字:javascript 停止 ImageButton Explorer net asp Internet      更新时间:2023-09-26

看起来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;">

这不是针对您的特定问题的解决方案,但这是您可能想要考虑的解决方案(我已经在几个地方使用了它):

  1. 让你的"删除"按钮变成一个简单的图片,而不是一个回复按钮。
  2. 给它一个调用本地javascript函数的OnClientClick(传递相关数据-至少,要删除的项目的ID)。
  3. 本地JS函数将相关数据加载到一个隐藏的变量中(因此它可以在你的代码后面访问),并打开jQuery确认对话框。
  4. 该对话框配置没有jquery-dialog按钮。相反,启动的DIV包含两个asp:Buttons: ok和cancel。取消按钮只是关闭jQuery对话框。OK按钮是一个普通的回发按钮,具有服务器端的OnClick功能。
  5. 因此,只有当用户单击确认对话框上的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
}