Invoke button click event from javascript in asp.net

In this blog post, I will show you how to trigger button click events from javascript. To understand The problem let’s create a simple page and put the following logic inside. The code is self-explanatory.

  function TrigButton() {
            if (window.event.keyCode == 13) {
                if (document.getElementById('txt').value.length > 0) {
                    document.getElementById('bt').focus();
                    document.getElementById('bt').click();
                }
            }
        }
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>javascript function for invoking btnclick event</title>

    <script language="javascript" type="text/javascript">
        function TrigButton() {
            if (window.event.keyCode == 13) {
                if (document.getElementById('txt').value.length > 0) {
                    document.getElementById('bt').focus();
                    document.getElementById('bt').click();
                }
            }
        }
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:textbox id="txt" runat="server" onkeydown="TrigButton()"></asp:textbox>
        <asp:button id="bt" runat="server" text="Button" onclientclick="javascript:alert('Button Triggered');"
            onclick="bt_Click" />
    </div>
    </form>
</body>
</html>

Post a Comment

Please do not post any spam link in the comment box😊

Previous Post Next Post

Blog ads

CodeGuru