开发者

OnKeyPress event not being triggered for ListBox

I'm trying to get the OnKeyPress event to work for a ASP.NET ListBox which renders itself as a SELECT element in HTML. After doing some search, a couple of site seems to suggest that it is supported, but the event just does not seem to trigger and call my JavaScript code. I开发者_JS百科t seems to trigger for another edit box on the same page, but does not do it for the SELECT element.

I want to use the OnKeyPress event so I could retrieve the ASCII code, whereas OnKeyDown and OnKeyUp returns the key code instead.

I'm currently using IE7 and I was wondering if the OnKeyPress event is supported by SELECT elements?

Here is how I am doing it in the aspx file:

<asp:ListBox ID="myListBox" runat="server" Width="306px" SelectionMode="Single"
  Rows="15" CssClass="myListBoxStyle" DataSourceID="myDataSource"
  onchange="myListBoxOnChange(this)"
  onkeypress="myListBoxOnKeyPress(this)" />


In your codebehind (page_load for ex.) specify

yourSelectList.Attributes.Add("onkeypress", "YourJavascriptMethodCallHere()");

this will call the client side javascript. If you want to process the key on the server side (added for additional info), check out: http://forums.asp.net/t/1281277.aspx/1

DropDownList1.Attributes.Add("onkeypress", Page.GetPostBackEventReference(DropDownList1,"onkeypress"));


My bad. It seems that I had an onkeydown="return false" in the ASPX code of my ListBox element which canceled the onkeypress event from happening.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜