Filter ListBox items based upon textbox from OnKeyUp?

醉酒当歌 提交于 2019-12-08 00:55:29

问题


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:


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());



回答2:


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.



来源:https://stackoverflow.com/questions/4031722/filter-listbox-items-based-upon-textbox-from-onkeyup

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!