Solved

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

Posted on 2013-05-14
10
729 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
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

773 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