Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The javascript used for executing the submit button when enter button press after filling the textbox field. but it is not working properly...What will be my mistake...plz help me to find it.
ASP.NET
<asp:TextBox ID="txtComment" runat="server" MaxLength="300"   onKeyPress="return processKey(event) ">
<script type="text/javascript">
      function processKey(e) {
      var key;
      if (window.event)
          key = window.event.keyCode;
      else
          key = e.which;

      if (key == 13) {

          var btn = document.getElementById('ctl00_btnsend');

          if (btn != null) {

              btn.click();

              event.keyCode = 0
          }
      }
}
</script>
Posted
Updated 17-Feb-13 20:32pm
v2

Call js function like this onBlur="functionName(event)" you can also use onChange="functionName"
 
Share this answer
 
v2
Comments
Member 9492907 18-Feb-13 3:34am    
not working these functions
Asim Mahmood 18-Feb-13 3:37am    
am currently using it
<asp:TextBox ID="TextBox1" runat="server" onChange="GetValues();" />
Your code seems fine. Have you added following attributes to your textbox?
C#
txtbox1.Attributes.Add("onKeyPress", 
                   "processKey('" + btnSearch.ClientID + "',event)"); //txtbox1 is an example, rename it your textbox name.



Also have a look at this link, same function in javascript has been written as the one you are using:

Capturing the Enter key to cause a button click[^]

Try this link as well:
using-javascript-detect-enter-key-pressed-event-in-a-textbox-and-perform-the-action/[^]
 
Share this answer
 
Here simple Jquery code to do the same...

XML
<script type="text/javascript">
$(document).ready(function(){
    $("#txtwhichpart").keypress(function(e){
        if (e.which == 13){
            // do your custom processing here
            $('#bsubmit_blist').click();
        }
    });
});
    </script>

txtwhichpart is text box id
bsubmit_blist is button id


//code behind
C#
protected void btnsearchhome_Click(object sender, EventArgs e)
{
   // here your code behind function of button
}
 
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