Solved

asp.net doing post back when html 5 textbox

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

734 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