Solved

asp.net doing post back when html 5 textbox

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

791 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