Topaz Filer - Email filing software
AutoComplete dropdownlist
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.aspnet.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

George D
GOOD ANSWER
i have a drop down list with about 1500 entries.. (list of
companies).. is there a way to be able to use something
similar to autocomplete in the dropdownlist.

For example, if I hit "s", the focus will go to the first
entry in the dropdownlist that starts with "s". But I
wanna be able to type "sf" for example and go to the first
entry that starts with "sf".

Obviously, the only value that should be saved is a valid
item from the dropdownlist. and until an item is selected,
any requiredfieldvalidator on the dropdownlist would get
fired.

Thanks!
Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Jim Ross [MVP] (VIP)
GOOD ANSWER
"George D" <Click here to reveal e-mail address> wrote:

[Original message clipped]

Here is something posted a couple of months ago by someone [I don't
recall who] from Microsoft. No guarantees:

<script type="text/javascript" language="javascript">

function SmartSelect(oInput, oSelect)
{
var sInput = String(oInput.value).toUpperCase();
var iLength = sInput.length;

if (iLength <= 0)
return -1;

var oOptions = oSelect.options;
var i, diff, bFound, sTemp;

var iHigh = oOptions.length - 1;
var iLow = 0;
var iCurrent = Math.floor((iHigh + 1) / 2);

bFound = false;
do
{
// Get the current option
sTemp = oOptions(iCurrent).value.toUpperCase();
var sSubstr = sTemp.substr(0, iLength);

if (sSubstr < sInput)
{
// Search the upper half of the branch
iLow = iCurrent + 1;
}
else if (sSubstr > sInput)
{
// Search the lower half of the branch
iHigh = iCurrent - 1;
}
else
{
bFound = true;
break;
}

// Pick the middle of the branch again
iCurrent = Math.floor(iLow + ((iHigh + 1) - iLow) / 2);

} while (iHigh >= iLow)

// Is there a better prefix match?
if (iLength < sTemp.length)
{
// Store the current old value
var iOld = iCurrent--;

// Now go back until we find one that doesn't match the prefix
while (iCurrent >= 0)
{
// Gone too far -- the prefix no longer matches.
if (oOptions(iCurrent).value.toUpperCase().substr(0, iLength) !=
sInput)
break;

iOld = iCurrent--;
}

iCurrent = iOld;
}

if (bFound)
return iCurrent;
else
return -1;
}

function DumbSelect(oInput, oSelect)
{
var sInput = String(oInput.value).toUpperCase();
var iLength = sInput.length;

if (iLength <= 0)
return -1;

var oOptions = oSelect.options;
var nElements = oOptions.length;

for (var iCurrent = 0; iCurrent < nElements; iCurrent++)
{
if (oOptions(iCurrent).value.substr(0, iLength).toUpperCase() ==
sInput)
{
break;
}
}

if (iCurrent < nElements)
return iCurrent;
else
return -1;
}

function BinarySearch()
{
var i = SmartSelect(document.all("binary"), document.all("list"));

// --------------------
// These two lines kill all the preceeding options.
// If the user makes a mistake, they will need to
// refresh the list somehow to get back the earlier
// entries
for (var idx = i - 1; idx >= 0; idx--)
document.all("list").remove(idx)

document.all("list").selectedIndex = 0;
// --------------------

// --------------------
// Use this instead just to jump to the
// first entry (comment out previous 3 lines
// and uncomment this one)

// document.all("list").selectedIndex = i;
// --------------------
}

function LinearSearch()
{
var i = DumbSelect(document.all("linear"), document.all("list"));

// --------------------
// These two lines kill all the preceeding options.
// If the user makes a mistake, they will need to
// refresh the list somehow to get back the earlier
// entries
for (var idx = i - 1; idx >= 0; idx--)
document.all("list").remove(idx)

document.all("list").selectedIndex = 0;
// --------------------

// --------------------
// Use this instead just to jump to the
// first entry (comment out previous 3 lines
// and uncomment this one)

// document.all("list").selectedIndex = i;
// --------------------
}

function GenerateList()
{
var theList = document.all("list")

// Clear current entries, if any
for (var i = theList.options.length - 1; i >= 0; i--)
theList.remove(i)

// Add new entries
for (var letter = 65; letter < 91; letter++)
{
var char = String.fromCharCode(letter)
for (var index = 0; index < 10; index++)
{
theList.options.add(new Option(char + index, char + index))
}
}
}
</script>
</head>
<body onload="GenerateList()">
<p>Generate the list: <input type="button" value="Generate List"
onclick="GenerateList()"></p>
<p>Binary search: <input type="text" id="binary"
onkeyup="BinarySearch()"></p>
<p>Linear search: <input type="text" id="linear"
onkeyup="LinearSearch()"></p>

<select id="list">
</select>
</body>
</html>

Jim Ross
MS MVP [ASP.NET][VC/MFC emeritus]

To send email, change 'lotsofspamthroughhere' to 'msn' but please ask all questions in the newsgroups, not via private mail
Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
George D
GOOD ANSWER
Jim,
thanks for the code snippet, I'll try this out. Did you
paste everything in? it seems like some stuff was cut off
the end.

thanks
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Jim Ross [MVP] (VIP)
GOOD ANSWER
That's all I have for that example.

"George D" <Click here to reveal e-mail address> wrote:

[Original message clipped]

Jim Ross
MS MVP [ASP.NET][VC/MFC emeritus]

To send email, change 'lotsofspamthroughhere' to 'msn' but please ask all questions in the newsgroups, not via private mail
Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
    
Lee Newton
GOOD ANSWER
[Original message clipped]

You need to use JavaScript to capture the keycode.

Reply to this message...
Vote that this is a GOOD answer... (1 vote from another user already)
 
 
 
System.Math
System.String




Ad
BootFX
Reliable and powerful .NET application framework.
Looking to invest in a major software project? Technical and commercial advice available here.
Other Helpful Sites
MBR 247
Topaz Filer
SharePoint Email Filing
Software Advisory Services
 
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734