Solved

Problem with asp.net textbox when hit enter key in text box

Posted on 2013-05-14
10
744 Views
Last Modified: 2013-05-16
Hello

i have one form in this there four text boxes all are asp text box
in this when i start filling data into text box with tab key there is no problem but when i hit the enter in any text box it directly submit the form and goes to home page.


<html xmlns="http://www.w3.org/1999/xhtml">
<body>
        <table >
            <tr>
                <td>    
                    First Name
                </td>
                <td>:</td>
                <td>
                    <asp:TextBox ID="txtFname" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>    
                    Last Name
                </td>
                <td>:</td>
                <td>
                    <asp:TextBox ID="txtLname" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>    
                    City
                </td>
                <td>:</td>
                <td>
                    <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>    
                  Address
                </td>
                <td>:</td>
                <td>
                    <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
                </td>
            </tr>
        </table>
</body>
</html>


Above is the code which i have used.
please help it urgent

Regards

Thanks in advance
0
Comment
Question by:atulmodi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 50 total points
ID: 39164190
This is a default behavior of asp textbox when user press Enter key the form gets submitted automatically.
to avoid this, for every asp:TextBox element add this attribute:
onkeydown = "return (event.keyCode!=13);" 

Open in new window

0
 
LVL 1

Expert Comment

by:muh_abbas
ID: 39164220
Hi  Atulmodi


just add the below code in code-behind file for every textbox for which you don't want to make enter-key work. Just replace TextBox1 with the name of your text box.


C#

TextBox1.Attributes.Add("onkeydown", "return (event.keyCode!=13);");

 

VB.Net

TextBox1.Attributes.Add("onkeydown", "return (event.keyCode!=13);")

Open in new window



2nd Option
----------------


If you want no text box entertain the enter key, add the following code in HTML code.


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">

    $(document).keypress(function(e) {
        if (e.which == 13) {
            return false;
        }
    });

</script>

Open in new window



PLEASE MARK THE ANSWER AS SOLUTION, IF YOU GOT YOUR ANSWER.

Regards,
Abbas
0
 

Author Comment

by:atulmodi
ID: 39164265
This is working fine but it is not acceptable or not suggest able  when i have more than 100 text box in same page or when project is enterprise level...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 42

Expert Comment

by:sedgwick
ID: 39164277
u can do it for all textbox in the form using jquery:

<script type="text/javascript">
$(function () {
       $(':text').bind('keydown', function (e) {
        //on keydown for all textboxes
        
               if (e.keyCode == 13) { //if this is enter key
                   e.preventDefault();
                   return false;
               }
               else
                  return true;
           }
       });
    });
</script>

Open in new window

0
 
LVL 5

Expert Comment

by:milindsaraswala
ID: 39164897
You can do like this for button which is pressed
Set the button's UseSubmitBehavior = "false"

Open in new window


Or
you can use java script
0
 
LVL 12

Expert Comment

by:funwithdotnet
ID: 39166914
You can assign the default button for the form to a button that is not visible/ returns false.
0
 

Author Comment

by:atulmodi
ID: 39167932
the code is not working for the all the text box when i put this script on to the application
this is not acceptable...
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39168155
Which code?
0
 
LVL 5

Expert Comment

by:milindsaraswala
ID: 39168273
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39170434
this was tested and worked:
 $('input[type="text"]').keydown(function(event){
   if(event.keyCode == 13) {
      event.preventDefault();
      return false;
    }
  });

Open in new window

0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Javascript question 8 44
Pass a js value to an html form 5 43
How to strip HTML from this JS code using regex 29 46
Ajax calendar distorted 4 24
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question