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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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 %>','')
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x