ASP.NET ImageButton onclick javascript没有触发

时间:2013-12-06 08:58:49

标签: javascript asp.net telerik

我的网站上有一堆非常相似的页面,在其中很多页面上,我有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;
}

1 个答案:

答案 0 :(得分:1)

不使用onclick属性,而是使用ImageButton的OnClientClick属性,如下面的

将以下代码替换为

后面的代码
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?");
相关问题