'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. It 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)" />


Solution 1:[1]

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"));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adam Tuliper