dropdownlist - autocomplete

zubie

Centurion
Joined
Jul 1, 2003
Messages
103
Location
Ireland
Hi all

I have a dropdownlist that can have upto 400 or so items in it.
I wish to use something list autocomplete to search through this list.
i.e. Type 'a' and get the first A in the list, type 'ae' and get the first 'ae' in the list

I know this is not possible without using Java script.
Does anyone out there have an example of this script and how to use it.

thanks
ZuBiE
 
Hi zubie,

i have written it for you:

Code:
<script language="javascript">
var str = "";
			var timerID = null;
			function jsKeyPress()
			{
				if (timerID != null){
					window.clearTimeout(timerID);} //delete old timer
				
				if (window.event.keyCode == 27){ //Esc pressed
					window.clearTimeout(timerID); str = ""; return false;}
					
				str += String.fromCharCode(window.event.keyCode);
				
				for(i=0; i<document.forms[0].ddlStelle.options.length; i++)
				{
					if (document.forms[0].ddlStelle.options[i].text.toLowerCase().indexOf(str) == 0)
					{
						document.forms[0].ddlStelle.selectedIndex = i;
						break;
					}
				}
				
				timerID = window.setTimeout("str='';", 500);
				return false;
			}
</script>
<asp:DropDownList onkeypress="return jsKeyPress();" id="ddlStelle" runat="server"></asp:DropDownList></td>

Regards, Stefan
 
Hi

I get the error "Could not find any attribute 'onkeypress' of element dropdowlist"

any ideas?

cheers
ZuBiE
 
Are you using ASP.net DropDownList or the normal HTML-Control?
Do you create the DropDownList synamic from code-behind or the placed it in the designer to your ASP.net Site?

Paste your code here, i will have a look...
 
I'm creating the DDL by dropping it on the screen then populating it from Code behind.

What part of the code do you want me to paste?
 
OK I seem to have the Javascript working for the onkeypress but I now recieve

Microsoft JScript runtime error: 'document.forms.0.ddlCustomer.options' is null or not an object

from the line
for(i=0; i<document.forms[0].ddlCustomer.options.length; i++)

thanks in advance for the help
ZuBiE
 
Not work :confused:

Not work :confused:
nothing happens ...???

ContactSearch.ascx
<script language="javascript">
var str = "";
var timerID = null;
function jsKeyPress()
{
if (timerID != null){
window.clearTimeout(timerID);} //delete old timer

if (window.event.keyCode == 27){ //Esc pressed
window.clearTimeout(timerID); str = ""; return false;}

str += String.fromCharCode(window.event.keyCode);

for(i=0; i<document.forms[0].name.options.length; i++)
{
if (document.forms[0].name.options.text.toLowerCase().indexOf(str) == 0)
{
document.forms[0].name.selectedIndex = i;
break;
}
}

timerID = window.setTimeout("str='';", 500);
return false;
}
</script>

<asp:dropdownlist id="name" runat="server" AutoPostBack="True" Width="300px" onkeypress="return jsKeyPress();"></asp:dropdownlist>
 
Last edited:
Back
Top