Aximili
Aximili

Reputation: 29474

ASP.NET Enter key triggering unwanted button

I have this on the top of a page (in MasterPage.master)

    <asp:Panel ID="panSearch" runat="server">
      <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
      <asp:ImageButton ID="btnSearch" runat="server" ImageUrl="~/images/iconSearch.gif" onclick="btnSearch_Click" />
    </asp:Panel>

The button keeps getting fired when I press the enter key on another TextBox down the page (in an aspx page)

<input type="text" id="txtTagName">
<input type="button" value="Tag" id="btnAddTagOk">

I couldn't find any JavaScript that does that.

Anyone has any idea why it's happening?

Upvotes: 2

Views: 3008

Answers (2)

Mikael Svenson
Mikael Svenson

Reputation: 39695

Hitting enter will submit the default form. If you have several controls which you want control over the enter button you need to hook in javascript on the control to intercept the enter key press, and then do your own logic.

Something like this

<input type="text" id="txtTagName" onkeydown="if (event.keyCode == 13) document.getElementById('btnAddTagOk').click()"/>

Upvotes: 2

Zain Shaikh
Zain Shaikh

Reputation: 6043

you might have some other button with its type as type="submit". The browser by default submits the form when enter is pressed on textboxes.

you might want to try following on your asp:panel

<asp:Panel ID="panSearch" runat="server" DefaultButton="btnSearch">

</asp:Panel>

Upvotes: 2

Related Questions