简体   繁体   中英

Is this a good workaround for enter button submitting in a text box?

So I have a page, it had multiple textboxes, linkbuttons, and buttons. Currently, a user clicks a linkbutton "Edit" which allows them to enter data in that row, via textboxes. However, upon pressing enter, the event for one of the buttons fired...this is not desired at all. Apparently, this is the default behavior built in?

Is it possible to associate each textbox so that pressing enter will act on a specific assigned linkbutton or button, without the textboxes being in separate forms?

If this is not possible, I'm going to try and create a button and make it the default form button, but disable and hide it, so that pressing enter submits nothing.

Thanks!

You can override the KeyPress event for each textbox to catch the enter key and then fire the code for the corresponding button.

if(e.KeyChar == '\n') 
   // execute button code

您可以将它们放在单独的面板中,并使用defaultbutton属性告诉面板在按下Enter键时要使用哪个按钮。

You can set the UseSubmitBehavior property to "false" on a Button control. This will cause the button to be rendered as an <input type="button" /> tag instead of an <input type="submit" /> tag. As a result, pressing the Enter key will not cause the button to be pressed, unless the button actually has the focus.

<asp:Button runat="server" UseSubmitBehavior="false" ... />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM