Click here to Skip to main content
15,861,340 members
Please Sign up or sign in to vote.
1.00/5 (1 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

JavaScript
<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 = window.open('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 
             //return;
         //I Want my focus should not lost and page load event should not call if Enter pressed 
          
            // return false;
         }
        }
        
        
 </script>  
    
    
    
<table>
    <tr>
        <td align="right" style="color: #000000; font-weight: bold">
            <span style="color:Red">*</span>Employee Id
        </td>
        <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" />
            Search
        </td>
    </tr>


C#
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
Comments
ridoy 18-Nov-12 0:48am    
so what's thee problem here?
Member-515487 18-Nov-12 1:00am    
page getting submitted on press enter on textbox

Using jquery ,we have done something simmiler to yours. we have place
JavaScript
$(window).keydown(function(event){
        if(event.keyCode == 13) {
          event.preventDefault();
          return false;
        }
      });

bunch lines of code in doucment.ready.
it basically suppress enter.
 
Share this answer
 
Comments
Member-515487 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.
 
Share this answer
 
JavaScript
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;
 
Share this answer
 
ascii code for 'enter' key is 13 use this no and put 'return false' at end of the statement.
 
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