here’s a dropdownlist that posts back on selected index changed.
<asp:DropDownList ID="_ddlClientType" runat="server" AutoPostBack = "true"
OnSelectedIndexChanged="ClientType_SelectedIndexChanged" >
<asp:ListItem Value="-1" Text="CANCEL.."></asp:ListItem>
<asp:ListItem Value="11" Text="External"></asp:ListItem>
<asp:ListItem Value="12" Text="Internal"></asp:ListItem>
<asp:ListItem Value="13" Text="TOM"></asp:ListItem>
</asp:DropDownList>
When using the mouse, everything works perfectly. However, I want user to be able to press key to select different items either by by using up/down arrow key. Also when when user type a letter, then the first item starting with that letter gets highlighted.
The problem is when a different item gets selected in the dropdownlist, the page posts back because of the “OnSelectedIndexChanged” event.
Is there a way to prevent the postback to occur when key are being pressed (as opposed to mouse being clicked), until the enter key is pressed?
EDIT
I’m able to display the alert prompt when a key is pressed
$('select').keypress(function () {
alert('A key was pressed')
});
Now, I need to know which key was pressed. Thus, I’ll be able to make a decision.
There is a bit of a mistake in your question:
The page is actually posting back because of
AutoPostBack = "true".First change AutoPostBack to False.
Then I would suggest that you handle the change event and the keypress event differently. If you handle the change event using jQuery and then submit inside that change event and also handle the keydown and filter for keys then you can submit only when you want to.
Here is an (updated) example. Replace the console.debug with submit of the form and you should be golden.
If you have multiple select boxes the code should be as follows:
Also be aware that if you have two entries that start with the letter typed, then you will select the last entry. A modification could be made to the code to allow for the box to be filtered.
Example for multiple boxes