views:

57

answers:

2

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 listbox is filtered to only display:

TextBox
3

ListBox
3434
3224

User then enters a 2 in the box:

Textbox
32

ListBox
3224

Is this possible in ASP.Net (not MVC2)? If so, is it best via callback in an UpdatePanel or javascript of somekind?

+1  A: 

There's a jQuery implementation of this in http://stackoverflow.com/questions/1688651/jquery-listbox-textbox-filter which should get you started, even if you don't want to use jQuery.

Stuart Dunkeld
+1  A: 

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());
Pramodh
Needed to add as ListItems, but other than that a nice solution :)
BlueChippy