ASPxListBox 项目转为字符串

发布于 2024-12-24 16:34:45 字数 534 浏览 2 评论 0原文

这应该很容易,但我没能做到这一点。我打开了 ASPxListBox 的复选框选项。所以我想在一个字符串中选择项目。像这样:

项目1,项目4,项目9

等。

请注意,它们应该被“,”分隔,并删除最后一个逗号。

尝试过类似的方法,但行不通:(

StringBuilder sb = new StringBuilder();
for (int i = 0; i < lb1.Items.Count; i++)
    sb.Append(lb1.Items[i].Selected ? lb1.Items[i].Text + "," : "");
TextBox1.Text = sb.ToString();

我之前问过类似的问题,但由于我没有提到这是 ASPxListBox,所以我得到的建议不起作用。

所以,这是 Visual Studio 2008、Devexpress 9.3 和 .NET Framework 3.5

谢谢。

This should be easy, but I'm failing to do this. I have ASPxListBox with checkbox option turned on. So I'd like to have selected items in one string. Like this:

item1,item4,item9

and so on.

Notice that they should be divided by "," and last comma deleted.

Tried something like this, but won't work:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < lb1.Items.Count; i++)
    sb.Append(lb1.Items[i].Selected ? lb1.Items[i].Text + "," : "");
TextBox1.Text = sb.ToString();

(I asked similar question before, but as I havent mention that this is ASPxListBox, suggestions I got wont works.

So, this is Visual Studio 2008, Devexpress 9.3 and .NET framework 3.5

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

余生一个溪 2024-12-31 16:34:45

我不熟悉 ASPxListBox,但是您不能循环遍历然后通过获取子字符串删除最后一个逗号吗?

 string csvList = sb.ToString();
 TextBox1.Text = csvList.Substring(0, csvList.LastIndexOf(','));

I'm not familiar with ASPxListBox, but couldn't you just loop through and then remove the last comma by getting the substring?

 string csvList = sb.ToString();
 TextBox1.Text = csvList.Substring(0, csvList.LastIndexOf(','));
烟花肆意 2024-12-31 16:34:45

@keyboardP:我认为你的解决方案相当不错,所以+1。

此外,您可以在 DevExpress 代码中心找到类似的解决方案:

http://www .devexpress.com/Support/Center/e/E2625.aspx

ASPxListBox listBox = instance;

string selectedItemsAsString = string.Empty;

foreach (ListEditItem item in listBox.SelectedItems)
    selectedItemsAsString += item.Value + ";";

if (selectedItemsAsString.Length > 0)
    selectedItemsAsString = selectedItemsAsString.Trim(new char[] { ';' });

@keyboardP: your solution is rather ok on my mind, so +1.

Moreover, you can find a similar solution in the DevExpress code central:

http://www.devexpress.com/Support/Center/e/E2625.aspx

ASPxListBox listBox = instance;

string selectedItemsAsString = string.Empty;

foreach (ListEditItem item in listBox.SelectedItems)
    selectedItemsAsString += item.Value + ";";

if (selectedItemsAsString.Length > 0)
    selectedItemsAsString = selectedItemsAsString.Trim(new char[] { ';' });
余罪 2024-12-31 16:34:45

我不熟悉 aspxlistbox 但你可以写这样的东西 -

    String Finaloutput="";
    for (int i = 0; i < lb1.Items.Count; i++)
    {
       string output= lb1.Items[i].Selected ? lb1.Items[i].Text + "," : "";
       Finaloutput += output;
    }
    TextBox1.Text = Finaloutput.TrimEnd(',');

I am not familiar with aspxlistbox but You can write something like this-

    String Finaloutput="";
    for (int i = 0; i < lb1.Items.Count; i++)
    {
       string output= lb1.Items[i].Selected ? lb1.Items[i].Text + "," : "";
       Finaloutput += output;
    }
    TextBox1.Text = Finaloutput.TrimEnd(',');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文