【问题标题】:ASP.NET ImageButton onclick javascript not firingASP.NET ImageButton onclick javascript 未触发
【发布时间】:2013-12-23 13:30:54
【问题描述】:

我的网站上有一堆非常相似的页面,在其中许多页面上,我都有删除或删除某种网格/树条目的 ImageButtons。对于所有这些,我想绑定一个基本上弹出“你真的要删除 x”对话框窗口的 javascript。

它几乎在每个页面上都运行良好,但在这个特定的页面上,脚本永远不会触发。检查按钮时,我看到正确列出了 onclick 事件。但是在单击按钮时,我在客户端上收到以下控制台错误:

ReferenceError: removeButtonConfirmation is not defined

没有其他事情发生。

.ascx 文件中的按钮:

<telerik:GridTemplateColumn UniqueName="DeleteColumn">
    <ItemTemplate>
           <asp:ImageButton runat="server" ID="DeleteButton" CommandName="Delete" />
    </ItemTemplate>
</telerik:GridTemplateColumn>

背后的代码:

var deleteButton = item["DeleteColumn"].FindControl("DeleteButton") as ImageButton;
deleteButton.ImageUrl = UrlHelper.SharedImage("delete2.png");
deleteButton.ToolTip = SiteTextResources.Administration_ShippingManager_Remove;
var onclickValue = String.Format("return removeButtonConfirmation('{0}');", "Are you sure you want to delete this?");
 deleteButton.Attributes.Add("onclick", onclickValue);

js:

function removeButtonConfirmation(message) {
    var result = window.confirm(message);
    if (result)
        return true;
    else
        return false;
}

【问题讨论】:

    标签: javascript asp.net telerik


    【解决方案1】:

    使用 ImageButton 的 OnClientClick 属性而不是添加 onclick 属性,如下所示

    替换你后面的代码

    var onclickValue = String.Format("return removeButtonConfirmation('{0}');", "Are you sure you want to delete this?");
     deleteButton.Attributes.Add("onclick", onclickValue);
    

    有了这个

    deleteButton.OnClientClick = String.Format("return removeButtonConfirmation('{0}');", "Are you sure you want to delete this?");
    

    【讨论】:

    • 谢谢,但我得到了同样的结果。
    猜你喜欢
    • 2011-12-15
    • 1970-01-01
    • 2011-02-05
    • 2021-05-07
    • 2013-04-30
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    • 2013-04-25
    相关资源
    最近更新 更多