I had asp page using Visual studio 2005 to develop. I had a dropdownlist box with more than hundred items list. I am looking a function which allow user to type on dropdownlist box and the dropdownlist box will automatically to search the related items.
I tried to use the JavaScript function, but it doesn’t work. Here is my JavaScript coding.
Expand|Select|Wrap|Line Numbers
- <SCRIPT TYPE="text/javascript">
- function onSelectKeyDown()
- {
- if(window.event.keyCode == 46)
- clr();
- }
- function selectKeyPress()
- {
- var sndr = window.event.srcElement;
- var pre = this.document.all["keys"].value;
- var key = window.event.keyCode;
- var char = String.fromCharCode(key);
- var re = new RegExp("^" + pre + char, "i");
- for(var i=0; i<sndr.options.length; i++)
- {
- if(re.test(sndr.options[i].text))
- {
- sndr.options[i].selected=true;
- document.all["keys"].value += char;
- window.event.returnValue = false;
- break;
- }
- }
- }
- function clr()
- {
- document.all["keys"].value = "";
- }
- </SCRIPT>
Expand|Select|Wrap|Line Numbers
- Me.dropdownlist.Attributes.Add("onkeypress", "return selectKeyPress();")
- Me.dropdownlist.Attributes.Add("onkeydown", "return selectKeydown();")
- Me.dropdownlist.Attributes.Add("onblur", "return clr();")
- Me.dropdownlist.Attributes.Add("onfocus", "return clr();")