Wanderer
Wanderer

Reputation: 7

Call a server side button click function from client side

I have a button like this:

<asp:LinkButton runat="server" ID="btnDeleteActivity" OnClientClick="return ConfirmDelete()" OnClick="btnDeleteActivity_Click">
     Delete activity
</asp:LinkButton>

and this HTML:

<i class="fa fa-trash" onclick="DeleteActivity()"></i>

in JS I have this:

function DeleteActivity() {
   document.getElementById('<%= btnDeleteActivity.ClientID %>').click();
}

The problem is that when I click on the i element, it calls only the OnClientClick of the button, and ignores the OnClick. (the JS function inside OnClientClick is just a simple return confirm('some message'))

I tried to delete the OnClientClick and test only with the OnClick on the button, but it still not calling it. When I test clicking directly on the button, it works perfectly.

Someone have an idea of what can be the problem?

Upvotes: 0

Views: 578

Answers (1)

Greg
Greg

Reputation: 4528

You can either alter JavaScript function ConfirmDelete() to return true when clicked by the i tag (by setting a global variable).

Or add another button which doesn't display or contain a JavaScript OnClick to handle the post back.

For example:

<asp:LinkButton
    style="display:none;"
    runat="server"
    ID="btnHiddenDeleteActivity" 
    OnClick="btnDeleteActivity_Click">
</asp:LinkButton>

Alternatively you can just call PostBack

function DeleteActivity() {
   __doPostBack('<%= btnDeleteActivity.UniqueID %>','')
}

Upvotes: 1

Related Questions