1

I'm trying to call c# event from javascript function. I tried this:

<script type="text/javascript">
    function LikeClick() {
        document.getElementById('<%=Helper.ClientID %>').click();
    }
</script>

<asp:Button ID="Helper" runat="server" OnClick="Helper_Click" />

But it doesn't trigger the c# event.

23
  • 1
    Possible duplicate of I can't click on an ASP:Button if it is hidden using jQuery? Commented Mar 31, 2017 at 13:44
  • TL;DR You have to hide the button client side using javascript/css - if you use Visible="False", it's not there in the page at all to be clicked. Commented Mar 31, 2017 at 13:45
  • I changed it to display none and visible=true but still nothing happans.. Commented Mar 31, 2017 at 13:47
  • 1
    wow. Change <asp:Button> to <asp:LinkButton> and it worked perfectly for me. Don't ask why :) Commented Mar 31, 2017 at 14:44
  • 1
    I don't know if I should laugh or cry haha. Thank you so much! Commented Mar 31, 2017 at 14:48

1 Answer 1

2

I believe a <asp:Button> will natively post the form back without a client side event handler. You need a control that does not natively post back but instead uses a client side click handler to call the __doPostBack() function.

Change

<asp:Button ID="Helper" runat="server" OnClick="Helper_Click" />

to this

<asp:LinkButton ID="Helper" runat="server" OnClick="Helper_Click" />

Sign up to request clarification or add additional context in comments.

Comments

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.