Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-05-14
10
Medium Priority
?
778 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 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…

721 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