Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

Hi zubie,

 

i have written it for you:

 

<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

Posted

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...

Posted

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?

Posted

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

  • 2 years later...
Posted (edited)

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>

Edited by Rea

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...