【问题标题】:Stop ImageButton OnCommand postback using javascript in asp.net 4.0 (Internet Explorer)在 asp.net 4.0 (Internet Explorer) 中使用 javascript 停止 ImageButton OnCommand 回发
【发布时间】:2011-10-17 15:30:03
【问题描述】:

看起来 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 更改为“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;">

【问题讨论】:

  • 你试过把开头改成 OnClientClick="return confirmDialog(... ?
  • 你能发布呈现的 HTML 吗?我怀疑你是对的,OnCommand 可能是罪魁祸首。查看发送到浏览器的实际 HTML/JS 可能有助于诊断。
  • Doozer: 我试过“return confirmDialog();”但它并没有改变 IE 如何不想阻止回发的发生。超级奇怪,这似乎与 IE 有关,因为 chrome 处理我所期望的一切。 MikeManne:我已经更新了我的问题以包含渲染的标记。

标签: javascript asp.net internet-explorer postback imagebutton


【解决方案1】:

这不是针对您的具体问题的解决方案,但它是您可能需要考虑的解决方法(我在几个地方使用过):

  1. 让您的“删除”按钮成为简单的图片,而不是回发按钮。
  2. 给它一个调用本地 javascript 函数的 OnClientClick(传递相关数据 - 至少是要删除的项目的 ID)。
  3. 该本地 JS 函数将相关数据加载到隐藏的 var 中(因此可以在您的代码隐藏中访问),并打开 jQuery 确认对话框。
  4. 该对话框配置为没有 jquery 对话框按钮。相反,启动的 DIV 包含两个 asp:Buttons:确定和取消。取消按钮只是关闭 jQuery 对话框。 OK 按钮是具有服务器端 OnClick 功能的普通回发按钮。
  5. 因此,只有当用户单击确认对话框上的“确定”按钮时,才会回发页面。 confirm-dialog-event 函数知道要删除哪个项目,因为该项目的 ID 在隐藏字段中。

这可能不是实现此行为的“正确”方式(从 ASP.NET 纯粹主义者的角度来看)。但是,随着变通方法的进行,我认为这并不太痛苦。根据我的经验,它似乎适用于所有浏览器(当然,假设它们启用了 JS)。 HTH。

【讨论】:

    【解决方案2】:

    You're 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
    }
    

    【讨论】:

    • 嗨,詹姆斯,感谢您的回复。我总是返回 false,因为 UI Dialog 不像原生 JS confirm() 那样持有 UI 线程。本质上必须发生的是,我必须阻止回发的执行,并且当触发“OK”事件时,我需要重新启动并导致原始回发。现在,只需让 OnClientClick="return false;"甚至没有阻止我的 OnCommand 事件在 IE 中触发(在其他浏览器中有效)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    相关资源
    最近更新 更多