开发者

ASP.NET Enter key triggering unwanted button

开发者 https://www.devze.com 2023-01-26 15:42 出处:网络
I have this on the top of a page (in MasterPage.master) <asp:Panel ID=\"panSearch\" runat=\"server\">

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="~/ima开发者_如何学编程ges/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?


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()"/>


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号