0

Code :

<form id="form1" runat="server">
<div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</div>
</form>

if I click on an input field and I press Enter, it submits nothing. Why? And how can I decide which page to call, pressing Enter, for each input box?

markzzz
  • 47,390
  • 120
  • 299
  • 507

3 Answers3

2

you set the forms default button:

<form id="Form1"
    defaultbutton="SubmitButton"
    runat="server">

Or you can do it in code - Page_load:

Page.Form.DefaultButton = btnSearch.UniqueID;

Another helpful tip is that you can set the default button on asp:panel's too. Wrap your 'forms' into a Panel or Multiple panels in form with will give your ability to set multiple submit button per Panel Control:

<asp:Panel runat="server" id="pnlForm1" DefaultButton="btnSubmit">
   <asp:Button runat="server" id="btnSubmit"/>
</asp:Panel>
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
1

You can put both boxes to separate Panel and set DefaultButton for each Panel.

UPDATE DefaultButton will work if you use Button, LinkButton works only in IE, there are some workarounds :

set linkbutton as default button for asp:panel in asp.net

Reusable Page_PreRender function in asp.net

Community
  • 1
  • 1
Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102
1

Use Jquery to resolve it:

$(document).ready(function () {
    $('body').bind('keypress', function (e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        var button = $("#<%= SubmitButton.ClientID %>");
        if (button != null && code == 13) {
            button.click();
        }
    });
})

or in C#

Page.Form.DefaultButton = SubmitButton.UniqueID;

HtmlForm.DefaultButton Property

Using Panel.DefaultButton property with LinkButton control in ASP.NET

Eugene Trofimenko
  • 1,611
  • 1
  • 13
  • 19