Solved

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

Posted on 2013-05-14
10
717 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:
sedgwick earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 12

Expert Comment

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

Author Comment

by:atulmodi
Comment Utility
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
Comment Utility
Which code?
0
 
LVL 5

Expert Comment

by:milindsaraswala
Comment Utility
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now