Click here to Skip to main content
12,064,498 members (45,820 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET Javascript
I want to when i press enter focus should not lost from current text box
or page load event should not call

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <script src="Scripts/validation.js" type="text/javascript"></script>

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            clear();//my fuctn that clear all values 
           
            txtEmpId.Attributes.Add("onkeypress", "javascript:return alphanumeric(event);"); //lets say i have preform some validations
           
            txtEmpId.Attributes.Add("onkeydown", "EmpId()"); //one pop up help aapers 
           
            btnEmpHelp.Attributes.Add("onclick", "EmpHelp()"); //that help button
           
        }
 
      
    }
 
  protected void txtEmpId_TextChanged(object sender, EventArgs e)
    {
      //lets say my Populated code
    }
Posted 17-Nov-12 19:14pm
Comments
ridoy 18-Nov-12 0:48am
   
so what's thee problem here?
Member 9472024 18-Nov-12 1:00am
   
page getting submitted on press enter on textbox
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Using jquery ,we have done something simmiler to yours. we have place
$(window).keydown(function(event){
        if(event.keyCode == 13) {
          event.preventDefault();
          return false;
        }
      });
bunch lines of code in doucment.ready.
it basically suppress enter.
  Permalink  
Comments
sushantraje2000 18-Nov-12 23:09pm
   
it will disable button behavior also
deepak.m.shrma 18-Nov-12 23:24pm
   
yup!! it disable button bhvr.. that is what you want. Button default behavior is to submit form. that is what you mention in type=submit. you have to submit your form onclick event.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

function stopRKey(evt) {
   var evt = (evt) ? evt : ((event) ? event : null);
   var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
   if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}
 
document.onkeydown = stopRKey;
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

ascii code for 'enter' key is 13 use this no and put 'return false' at end of the statement.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160204.4 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100