locked
JavaScipt not calling asp.net function RRS feed

  • Question

  • User-29703693 posted

    I have the following in a user control:

    <asp:Button runat="server" ID="btnSubmitInvisible" OnClientClick="javascript:alert('here');" OnClick="btnSubmit_Click" />
    <button id="btnSubmit" onclick="ClickBtn()">Submit</button>
    
    <script type="text/javascript">
        function ClickBtn() {
            document.getElementById('<%= btnSubmitInvisible.ClientID %>').click();
        }
    </script>

    If I click on btnSubmitInvisible directly it works as expected: I get the alert message and the btnSubmit_Click is called. 

    However, if I click on btnSubmit, I get the alert message but btnSubmit_Click never gets called.  

    Any ideas why or how I can troubleshoot?

    Wednesday, November 11, 2020 3:57 PM

Answers

  • User-474980206 posted

    that is because the the btnSubmit is doing the post back rather than the server button. either make it a button or cancel submit:

    <button type="button" id="btnSubmit" onclick="ClickBtn()">Submit</button>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 11, 2020 10:53 PM

All replies

  • User-474980206 posted

    that is because the the btnSubmit is doing the post back rather than the server button. either make it a button or cancel submit:

    <button type="button" id="btnSubmit" onclick="ClickBtn()">Submit</button>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 11, 2020 10:53 PM
  • User-29703693 posted

    Thanks Bruce.  Much appreciated

    Thursday, November 12, 2020 4:17 PM