bucurvlad
bucurvlad

Reputation: 119

Pressing enter submits wrong button

I have two controls of type asp:ImageButton on masterPage. my login page contains a asp:Login control with two textboxes for username and password and a asp:Button for logging in.

If I press Enter after completing the two textboxed, it will trigger the ImageButton's method which refreshes the page.

Upvotes: 1

Views: 689

Answers (1)

Martin Parenteau
Martin Parenteau

Reputation: 73731

You can use the property DefaultButton to define, at the form level or inside a Panel, the button that will be "clicked" by default when you press Enter:

<form id="form1" runat="server" defaultbutton="btn5">
    <asp:TextBox ID="txtBox0" runat="server" />
    <asp:Panel runat="server" DefaultButton="btn2">
        <asp:TextBox ID="txtBox1" runat="server" />
        <asp:Button ID="btn1" runat="server" Text="Button1" />
        <asp:Button ID="btn2" runat="server" Text="Button2" />
    </asp:Panel>
    <asp:TextBox ID="txtBox2" runat="server" />
    <asp:Button ID="btn3" runat="server" Text="Button3" />
    <asp:Button ID="btn4" runat="server" Text="Button4" />
    <asp:Button ID="btn5" runat="server" Text="Button5" />
</form>

In the example above:

  • btn2 will be "clicked" by pressing Enter in txtBox1
  • btn5 will be "clicked" by pressing Enter in txtBox0 or in txtBox2

Upvotes: 2

Related Questions