Click here to Skip to main content
15,896,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

Are you familiar with LIKE condition in SQL? Is it possible to do the same approach to a bound dropdown list? like if I search for 'CH', the program will find the 1st existing item in dropdown list that starts with 'CH' and selects it.

Thanks for you help!

What I have tried:

I have no idea about what to do here. If this is not even possible, i will just select index 0.
Posted
Updated 3-Jan-17 23:08pm

Maybe you can use the client side script/plugin to filter the bounded dropdownlist?

Here are some example:

http://jsearchdropdown.sourceforge.net/
https://www.sitepoint.com/13-jquery-selectboxdrop-down-plugins/
 
Share this answer
 
v2
Hope following works for you

C#
foreach (ListItem item in this.cmb.Items)
{
    if (item.Text.StartsWith("CH", StringComparison.OrdinalIgnoreCase))
    {
        item.Selected = true;
        break;
    }
    else
        item.Selected = false;
    }
}

This is ofcourse after you bind your combo box with the data from the database
 
Share this answer
 
v2
Quote:
Assuming the list is already data bound you can simply set the SelectedValue property on your dropdown list.


Quote:
list.DataSource = GetListItems(); // <-- Get your data from somewhere.
list.DataValueField = "ValueProperty";
list.DataTextField = "TextProperty";
list.DataBind();

list.SelectedValue = myValue.ToString();
 
Share this answer
 
You can use the HTML5 datalist element. The element specifies a list of pre-defined options that are bound to an element. It looks like the element but with the added auto-complete function. When a user types into the text field, the matching options from the datalist will appear in a drop-down list. An example from my Formidable Forms with HTML5[^] :
<label for="zodiac">Zodiac</label>
<p>
<input list="zodiac" name="zodiac">
<datalist id="zodiac">
<option value="Aries">
<option value="Taurus">
<option value="Gemini">
<option value="Cancer">
<option value="Leo">
<option value="Virgo">
<option value="Libra">
<option value="Scorpio">
<option value="Sagittarius">
<option value="Capricorn">
<option value="Aquarius">
<option value="Pisces">
</datalist>
A demo at JSFiddle[^]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900