Filter ListBox items based upon textbox from OnKeyUp?
I have a ListBox containing a large number of items, which are all account numbers and so hard to search through.
Is it possible to have the items "filtered" as the user types into a textbox, so that only items that match what has been entered so far are displayed?
e.g.
List Box
2342 3434 2332 3224
User then enters 3 in the texbox - onKeyUp the lis开发者_Go百科tbox is filtered to only display:
TextBox
3ListBox
3434 3224
User then enters a 2 in the box:
Textbox
32ListBox
3224
Is this possible in ASP.Net (not MVC2)? If so, is it best via callback in an UpdatePanel or javascript of somekind?
Here is a sample solution
private void textBox1_TextChanged(object sender, EventArgs e)
{
listBox1.Items.Clear();
List<String> lst = new List<string> {"2342","3434","2332","3224"};
listBox1.Items.AddRange(lst.Where(X => X.StartsWith(textBox1.Text)).ToArray());
}
And one more
listBox1.Items.AddRange(listBox1.Items.Cast<String>().Where(X=>X.StartsWith(textBox1.Text)).ToArray());
There's a jQuery implementation of this in Jquery Listbox / Textbox filter which should get you started, even if you don't want to use jQuery.
精彩评论