1

I have a bunch of very similar pages on my site, and on many of them, I have ImageButtons that remove or delete some sort of grid/tree entry. For all of those, I want to tie a javascript that essentially pops up a "Do you really want to delete x" dialog window.

It's working fine on almost every page, but on this particular one, the script never fires. When inspecting the button, I see the onclick event corretly listed. But upon clicking the button, I get the following console error on the client:

ReferenceError: removeButtonConfirmation is not defined

And nothing else happens.

The button in the .ascx file:

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

The code behind:

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);

The js:

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

1 Answer 1

1

Instead of adding onclick attribute use OnClientClick property of ImageButton like following

Replace you following code behind line

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

With this

deleteButton.OnClientClick = String.Format("return removeButtonConfirmation('{0}');", "Are you sure you want to delete this?");
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but I get the same result with this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.