Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-05-14
10
Medium Priority
?
792 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
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 42

Accepted Solution

by:
Meir Rivkin earned 100 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:Meir Rivkin
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:Meir Rivkin
ID: 39168155
Which code?
0
 
LVL 5

Expert Comment

by:milindsaraswala
ID: 39168273
0
 
LVL 42

Expert Comment

by:Meir Rivkin
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

Industry Leaders: 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!

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…
Suggested Courses

927 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