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

x
?
Solved

asp.net doing post back when html 5 textbox

Posted on 2014-09-29
3
Medium Priority
?
691 Views
Last Modified: 2014-09-30
I have one web form which has only one textbox which is also not server control
now the strange issue is when I enter any text in the textbox and press enter then webform is doing a postback.

On the otherhand if I add one more textbox with different id
then web form does not do post back on either of textbox.

I need to know why it is doing postback at the first place where I have only textbox which even not a server control.
my code is below



Web form code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm5.aspx.cs" Inherits="Documentation.WebForm5" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="text" class="form-control" id="title" placeholder="Title" />
    </div>
    </form>
</body>
</html>

Open in new window


server side code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Documentation
{
    public partial class WebForm5 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string s = "d";
        }
    }
}

Open in new window

0
Comment
Question by:yadavdep
[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
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
Vishal Patil earned 2000 total points
ID: 40351852
Hello yadavdep,

Add following script in <head> section to overcome this issue.
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).keypress(function (e) {
            if (e.keyCode === 13) {
                e.preventDefault();
                return false;
            }
        });
    </script>

Open in new window

0
 

Author Comment

by:yadavdep
ID: 40353864
Hi Vishal,

 Thanks for your work around but I wanted to know why asp.net is doing it. Is it a bug or does it have a logical answer.
0
 
LVL 2

Assisted Solution

by:Vishal Patil
Vishal Patil earned 2000 total points
ID: 40353912
Hello yadavdep,

Generally most web browsers will detect when a form has only one Text-box and will automatically submit the form when enter is pressed.
If you have multiple Text-boxes or multiple form buttons, the behavior varies.

To solve this issue(using only one Text-box on page), you may need to look at a JavaScript solution to catch the enter key being pressed.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

715 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