Click here to Skip to main content
12,943,571 members (49,212 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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>
    <script language="javascript"  type="text/javascript">

        function EmpHelp() {
            popup ='EmpMstrHelp.aspx', null, 'height=320,  width=320,status= no, resizable= no, scrollbars=no, toolbar=no,location=center,menubar=no, top=100, left=300');
            common = "EmpHelp"
            return false;
        function EmpId() {
           // alert('ss');
       //  return (event.keyCode!=13);
         var key = event.keyCode;
        // alert(key);
         if (key == 113 && key != 13) {
             document.getElementById('<%=btnEmpHelp.ClientID %>').click();
         else {
           //document.getElementById('<%=txtEmpId.ClientID %>').focus();//its also not working and i want for all text box not for only specific one 
         //I Want my focus should not lost and page load event should not call if Enter pressed 
            // return false;
        <td align="right" style="color: #000000; font-weight: bold">
            <span style="color:Red">*</span>Employee Id
        <td align="left" style="color: #000000; font-weight: bold">
            <asp:TextBox ID="txtEmpId" runat="server" Width="50px" MaxLength="5" TabIndex="1" 
                    AutoPostBack="True" ontextchanged="txtEmpId_TextChanged"></asp:TextBox>
             <asp:Image ID="btnEmpHelp" runat="server" ImageUrl="~/help.gif" Width="20px" />

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 18:14pm
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
Please Sign up or sign in to vote.

Solution 1

Using jquery ,we have done something simmiler to yours. we have place
        if(event.keyCode == 13) {
          return false;

bunch lines of code in doucment.ready.
it basically suppress enter.
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
Please Sign up or sign in to vote.

Solution 2

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

Solution 4

function stopRKey(evt) {
   var evt = (evt) ? evt : ((event) ? event : null);
   var node = ( ? : ((evt.srcElement) ? evt.srcElement : null);
   if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
document.onkeydown = stopRKey;
Rate this: bad
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.

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
OriginalGriff 4,643
CHill60 2,970
Maciej Los 2,348
Jochen Arndt 1,900
ppolymorphe 1,765

Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2017
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