tags:

views:

1074

answers:

3

I have a couple of asp.net pages with TextBox controls on them. Some of them postback on enter (within a TextBox control) and others don't. For the life of me, I can't figure out why or why not...

I don't think this has anything to do with asp.net. Not using ajax etc. Tried playing with a very simple html page and still can't figure it out.

Maybe within or not within a <form> tag? But all my asp:textbox controls are within the standard form tag and some do and some don't. Not related to autoPostback from what I can see.

EDIT:

Sample markup which causes a post back:

<html>
<body>

<form>
<input type=text />
</form>

</body>
</html>

I have asp.net pages that definately don't post back. The code is extensive and I can't figure out why not.

EDIT2:

How can I prevent a postback?

A: 

If the textbox control is set to autopostback it will postback on every keypress (thus allowing the firing of the TextChanged event).

Next, text boxes that have their Multiline property set to false will appear to postback on Enter.. what is actually happening is a form submit as defined by the behaviour within the browser.

Boxes that have Multiline set to true will not cause a form submit on Enter.

Boo
+1  A: 

Generally most web browsers will detect when a form has only one textbox and will automatically submit the form when enter is pressed.

If you have multiple text boxes or multiple form buttoms, the behaviour varies. ASP.Net has the "defaultButton" mechanism for buttons, meaning you can specify which button is the submit button for a given panel in the case of the enter key being pressed. You could check out this post for more information on setting up your forms to correctly post back.

As Boo mentioned, this won't be triggered by multi-line textboxes.

If the behaviour is really crappy in your form between browsers, you may need to look at a javascript solution to catch the enter key being pressed, and "manually" submitting the form via javascript.

womp
+2  A: 

If you only have one textbox and don't wan't this behavior, you could always put a hidden input on the form.

<html>
    <body>
     <form>
      <input type="text" ></input>
      <input type="text" style="visibility:hidden"></input>
     </form>
    </body>
</html>

I've tried this in IE7 and FF3 and it works.

Chad
Once the solution became obvious, you provided a clean solution.
Andrew Robinson