[英]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.