0

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)" />
1

2 Answers 2

0

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"));
1
  • I did that in the page_load of my code behind and it isn't working: myListBox.Attributes.Add("onkeypress", "myListBoxOnKeyPress(this);"); Aug 26, 2011 at 5:21
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.