过滤掉的onkeyup基于文本框列表框的项目? [英] Filter ListBox items based upon textbox from OnKeyUp?
问题描述
我有一个包含大量项目,这是所有的帐号和这么辛苦的一个列表框中进行搜索。
是否有可能有项目过滤作为用户键入到一个文本框,这样只有匹配的内容迄今输入显示的项目?
例如
列表框结果
2342结果
3434结果
2332结果
3224
块引用>用户然后在texbox进入3 - 的onkeyup列表框被过滤,只显示:
文本框结果
3
的ListBox 结果
3434结果
3224
块引用>用户然后在框中输入一个2:
文本框结果
32
的ListBox 结果
3224
块引用>这是可能在ASP.Net(不MVC2)?
如果是这样,它是通过在一个UpdatePanel回调或JavaScript somekind的最好?解决方案下面是一个简单的解决方案
私人无效textBox1_TextChanged(对象发件人,EventArgs的发送)
{
listBox1.Items.Clear();
清单<串GT; LST =新的List<串GT; {2342,3434,2332,3224};
listBox1.Items.AddRange(lst.Where(X =方式> X.StartsWith(textBox1.Text))ToArray的()); }和多了一个
<$p$p><$c$c>listBox1.Items.AddRange(listBox1.Items.Cast<String>().Where(X=>X.StartsWith(textBox1.Text)).ToArray());
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
3224User then enters 3 in the texbox - onKeyUp the listbox is filtered to only display:
TextBox
3ListBox
3434
3224User then enters a 2 in the box:
Textbox
32ListBox
3224Is 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());
这篇关于过滤掉的onkeyup基于文本框列表框的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!