Solved

asp.net doing post back when html 5 textbox

Posted on 2014-09-29
3
619 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
  • 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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now