Solved

asp.net doing post back when html 5 textbox

Posted on 2014-09-29
3
659 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 500 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 500 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

733 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