Click here to Skip to main content
15,924,507 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I am currently developing a Vb.net application and i have a button on my aspx page called BtnAdd.

The Event is called btnAdd_click.

I would like to know how would i be able to call the event btnAdd_click when the user presses the enter key on the Keyboard

I Have tried the following Javascript code which works only the when the page is loaded,problem i am having is i use an asynchronous postback to refresh just the table when i need to add new rows to my table, and then the page needs to be reloaded before the script can work again.

XML
<script type="text/javascript" src="../jquery.min.js"></script>
    <script type="text/javascript">
      $(document).ready(function () { $("input").bind("keydown", function (event) { var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode)); if (keycode == 13) { document.getElementById('ctl00_PageContent_btnAdd').click(); return false; } else { return true; } }); });
    </script>



Please could you assit
Posted
Updated 26-Feb-14 19:18pm
v3

on your page load event add this code

this.TextBox1.Attributes.Add("onkeypress", "button_click(this,'" + this.Button1.ClientID + "')");



and write a javascript code like below in script

XML
<script>
    function button_click(objTextBox,objBtnID)
    {
        if(window.event.keyCode==13)
        {
            document.getElementById(objBtnID).focus();
            document.getElementById(objBtnID).click();
        }
    }
    </script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900