简体   繁体   English

使用键盘操作时,仅当用户按下Enter时,才会在列表框上触发SelectedIndexChanged事件

[英]Fire SelectedIndexChanged event on a listbox, when operating with keyboard, only when user presses Enter

I have a listbox that I populate from my codebehind. 我有一个列表框,该列表框是从我的代码后面填充的。 I have a handler for the SelectedIndexChanged event. 我有一个SelectedIndexChanged事件的处理程序。 When I use the mouse, everything works fine. 当我使用鼠标时,一切正常。 It is just a plain, standard listbox, no mysteries. 它只是一个普通的标准列表框,没有任何奥秘。

But when I try to use the keyboard, as soon as I arrow down even once, the SelectedIndexChanged event gets fired, because, of course, the selection is changing. 但是,当我尝试使用键盘时,即使我向下箭头一次,也会触发SelectedIndexChanged事件,因为,当然,选择会发生变化。 The list does not wait for me to actively select an item by pressing Enter. 该列表不等待我通过按Enter主动选择一个项目。 So if I want to select the fifth item, the event gets fired five times, once for each selection. 因此,如果我要选择第五项,则该事件被触发五次,每次选择一次。

Question: Is there any way to have a listbox select items only on Enter when operating it with the keyboard? 问题:使用键盘操作列表框时,是否有任何方法可以使列表框仅在Enter上选择项目? Or is there a different control I should be using to let the user select an item from a list? 还是我应该使用其他控件让用户从列表中选择一项?

Seems like this would be so necessary for accessibility that someone should have a solution. 似乎这样对于可访问性非常必要,因此有人应该有解决方案。 Here is my aspx code for the listbox. 这是我的列表框的aspx代码。

<asp:ListBox id="OwnersList" runat="server" AutoPostBack="True" class="dropdown-list StopClickPropagation" onblur='$(".dropdown-list").hide();' on OnSelectedIndexChanged="OwnerSelectionChanged">
</asp:ListBox>

AFAIK, listbox or any other asp.net control can meet your requirement without additional Javascript code. AFAIK,列表框或任何其他asp.net控件都可以满足您的要求,而无需其他Javascript代码。 In order to fulfill your requirement, you need to hook up onkeypress and onclick events on client side, and postback through __doPostBack method. 为了满足您的要求,您需要在客户端挂接onkeypress和onclick事件,并通过__doPostBack方法进行回发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户按下输入按钮wpf时,如何在键盘上触发按键事件 - How can I fire a key press event on a keyboard when a user presses enter button wpf 为什么在修改所选项目时,ListBox会触发SelectedIndexChanged事件? - Why does the SelectedIndexChanged event fire in a ListBox when the selected item is modified? 从列表中选择项目时,列表框selectedIndexChanged不会触发 - Listbox selectedIndexChanged doesn't fire when selecting an item from the list 在组合上设置DataSource时不要触发SelectedIndexChanged事件 - Don't fire the SelectedIndexChanged event when setting the DataSource on a combo 绑定列表更新时未触发 ListBox SelectedIndexChanged 事件 - ListBox SelectedIndexChanged event not fired when bound list updates 列表框:添加一项时调用事件SelectedIndexChanged - Listbox: Event SelectedIndexChanged called when add one item Fire comboBox SelectedIndexChanged仅来自用户点击 - Fire comboBox SelectedIndexChanged only from user click 仅当用户完成拖动轨迹栏时触发事件 - Fire event only when the user is done dragging the trackbar 仅当用户单击ListBox时才引发DrawItem事件 - DrawItem event is fired only when the user clicks the ListBox 用户按下网页返回时如何执行事件 - How to execute event when user presses return on web page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM