如何从列表项中获取值? [英] How to get values from list items?
本文介绍了如何从列表项中获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有四个带有多个选择的列表框。
列出a,b,c,d;
i通过以下代码获取其值。< br $>
I have four lists boxes with multiple selection.
list a,b,c,d;
i am getting its values by below code.
string a = String.Join(",", listA.Items.OfType<ListItem>().Where(r => r.Selected).Select(r => r.Text));
string b = String.Join(",", listB.Items.OfType<ListItem>().Where(r => r.Selected).Select(r => r.Text));
string c = String.Join(",", listC.Items.OfType<ListItem>().Where(r => r.Selected).Select(r => r.Text));
string d = String.Join(",", listD.Items.OfType<ListItem>().Where(r => r.Selected).Select(r => r.Text));
string values=a+b+c+d;
我的问题是,当我选择值时,结果如下。
,b,c,d OR
a ,, c,d OR
a,b,c,
我需要结果如
b,c,d或
a,c,d
a,b,c
有无用的逗号我们如何删除它。
我尝试过:
My question is when i select values it result like.
,b,c,d OR
a,,c,d OR
a,b,c,
I need the result like
b,c,d OR
a,c,d
a,b,c
There are useless commas how we remove it.
What I have tried:
<pre lang="C#">
string a = String.Join(",", listA.Items.OfType<ListItem>().Where(r => r.Selected).Select(r => r.Text));
string b = String.Join(",", listB.Items.OfType<ListItem>().Where(r => r.Selected).Select(r => r.Text));
string c = String.Join(",", listC.Items.OfType<ListItem>().Where(r => r.Selected).Select(r => r.Text));
string d = String.Join(",", listD.Items.OfType<ListItem>().Where(r => r.Selected).Select(r => r.Text));
string values=a+b+c+d;
</pre>
推荐答案
试试这个
try this
string a = String.Join(",", listA.Items.OfType<ListItem>().Where(r => r.Selected && !string.IsNullOrWhiteSpace(r.Text)).Select(r => r.Text));
您可以使用 String.TrimEnd方法 [ ^ ]
类似的东西 -
You can use String.TrimEnd Method [^]
Something like -
values = values.TrimEnd(',');
希望,它有助于:)
Hope, it helps :)
这篇关于如何从列表项中获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文