473,466 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ASP.NET Dropdownlist


Hi,

I am writing to ask if anyone knows if it is possible to create a
dynamic, searchable dropdownlist in ASP.Net.

I have a questionnaire with a dropdownlist that is populated with a list
of countries from a database table. I want to the user to be able to
type the first few letters of the country and for the ddl to jump to the
countries that begin with the letters typed in.

Any help / info on this subject would be much appreciated.

Thanks,
Kevin.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #1
4 1694
Try this JavaScript method..(works for a listbox)

call "return SmartSearch_ListKeyDown(this);" in your
keydown event of the control and you get fast searching.

The JavaScript (based on a variety of newsgroup postings)..

//================================================== =======
===
// Perform a binary search on a selectbox and return the
index
// of a match
function SmartSelect(searchString, oSelect)
{
var sInput = String(searchString).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).text.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;
}
//================================================== ==
//Some globals
var searchString="";
var lastSearch="";
var lastObjID="";

//================================================== ==
//Reset the search string if it has not been changed in the
//last 2 seconds
function ClearSearchString()
{
date= new Date();
var tdiff= date.valueOf() - lastSearch;

if(tdiff<1000*2)
{
setTimeout("ClearSearchString();",1000);
}
else
{
searchString="";
}
}
//================================================== =======
===============
//Pulse the selection of particular item within a listbox
which has
//the affect of scrolling to that item
function PulseSelection(oOption)
{
var bSelected=oOption.selected;

oOption.selected=false;
oOption.selected=true;
oOption.selected=bSelected;
}
//================================================== =======
===============
//Called whenever a key is pressed to provide dynamic
searching on an object
function SmartSearch_ListKeyDown(lb)
{
if(lastObjID!=lb.id)
{ //we're searching a different control so
reset search string
lastObjID=lb.id;
ClearSearchString();
}

//check for any 'special' keys and pass them
through to the object to handle
switch(event.keyCode)
{
case 32:
if(searchString.length<=0)return
true;//space key
break;
case 38://cursor up
case 40://cursor down
return true;
}

searchString += String.fromCharCode(event.keyCode);

var i = SmartSelect( searchString, lb);
if(i>=0)
{
PulseSelection(lb.options[i]);
date= new Date();
lastSearch = date.valueOf();
}

setTimeout("ClearSearchString();",1000);

return(false);//return false to stop default
keypress functionality
}
//===============================================

Nov 18 '05 #2
Hi,

Thanks for the reply.

Could you tell me what web controls provide the keydown event.

Thanks Again.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #3
Add the logic to your controls within ASP.Net

MyListbox.Attributes.Add("onkeydown", "return
SmartSearch_ListKeyDown(this);");

Note: All the actual logic and events are javascript based
not .Net.
-----Original Message-----
Try this JavaScript method..(works for a listbox)

call "return SmartSearch_ListKeyDown(this);" in your
keydown event of the control and you get fast searching.

The JavaScript (based on a variety of newsgroup postings)..
//================================================== ====== ====
// Perform a binary search on a selectbox and return the
index
// of a match
function SmartSelect(searchString, oSelect)
{
var sInput = String(searchString).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).text.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;
}
//================================================== ==
//Some globals
var searchString="";
var lastSearch="";
var lastObjID="";

//================================================== ==
//Reset the search string if it has not been changed in the//last 2 seconds
function ClearSearchString()
{
date= new Date();
var tdiff= date.valueOf() - lastSearch;

if(tdiff<1000*2)
{
setTimeout("ClearSearchString();",1000);
}
else
{
searchString="";
}
}
//================================================== ====== ================
//Pulse the selection of particular item within a listbox
which has
//the affect of scrolling to that item
function PulseSelection(oOption)
{
var bSelected=oOption.selected;

oOption.selected=false;
oOption.selected=true;
oOption.selected=bSelected;
}
//================================================== ====== ================
//Called whenever a key is pressed to provide dynamic
searching on an object
function SmartSearch_ListKeyDown(lb)
{
if(lastObjID!=lb.id)
{ //we're searching a different control so
reset search string
lastObjID=lb.id;
ClearSearchString();
}

//check for any 'special' keys and pass them
through to the object to handle
switch(event.keyCode)
{
case 32:
if(searchString.length<=0)return
true;//space key
break;
case 38://cursor up
case 40://cursor down
return true;
}

searchString += String.fromCharCode(event.keyCode);

var i = SmartSelect( searchString, lb);
if(i>=0)
{
PulseSelection(lb.options[i]);
date= new Date();
lastSearch = date.valueOf();
}

setTimeout("ClearSearchString();",1000);

return(false);//return false to stop default
keypress functionality
}
//===============================================

.

Nov 18 '05 #4

Hi,

Thanks a lot, I got it working and it works quite well. A handy little
piece of code.

Thanks again.

Kevin.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Stanley J Mroczek | last post by:
How do you load a dropdownlist when edit is clicked in a datagrid ? <Columns> <asp:BoundColumn DataField="OptionDescription" ItemStyle-Wrap="True" HeaderText="Option...
4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
2
by: Dominic | last post by:
Hi guys, I'm not sure if this question belongs to FAQ, but I couldn't find a concrete answer. I created a Datagrid control using ItemTemplate, but it's NOT a in-place editing datagrid. One of...
2
by: Shiju Poyilil | last post by:
Hello, I have a datagrid with only one row and its having 2 dropdownlists, I need to populate the secodn dropdownlist on the basis of the selection in the first dropdown. but I am not able to...
10
by: Sacha Korell | last post by:
I'm trying to load a drop-down list with all DropDownList control names from another page. How would I be able to find those DropDownList controls? The FindControl method will only find a...
15
by: glenn | last post by:
Hi folks, I have a DropDownList in a DataGrid that is populated from records in a database. I want to add a value that might be a string such as "Select a Company" for the first item since an...
1
by: MaryamSh | last post by:
Hi, I am creating a Dynamic Search in my application. I create a user control and in Page_load event I create a dynamic dropdownlist and 2 dynamic button (Add,Remove) By pressing Add button...
0
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I...
1
by: Brett | last post by:
I have a DropDownList in an ASP.NET web form that is populated with items from a lookup table by binding that DropDownList to a SqlDataSource. However, the items in the lookup table can change over...
0
by: asmx126453 | last post by:
Hey mensen I am having some big troubles here i tryd solving it myself with internet for 2 days but i kind fix it. Its about this i have a DotNet project that alrydi is online and working for...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.