如何在asp.net列表框中获取多个选定项并在文本框中显示? [英] How to get multiple selected items in asp.net listbox and show in a textbox?

查看:216
本文介绍了如何在asp.net列表框中获取多个选定项并在文本框中显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将列表框中的选定项添加到文本框,并且逗号之间要用逗号分隔。但是它每次都只读取所选项目的第一个元素。如果我选​​择三个按住ctrl的值,则它只能通过所选项目的第一个元素

i am trying to add the selected items from the listbox to the textbox with the comma seperated between each other. but it is only reading the first element of the selected items every time.if i select three values holding ctrl its only passing the fist elemnt of selected items

if (ListBox1.SelectedItem != null)
        {

            //   int count = ListBox1.SelectedItems.Count;
            if (TextBox1.Text == "")
                TextBox1.Text += ListBox1.SelectedItem.ToString();
            else
                TextBox1.Text += "," + ListBox1.SelectedItem.ToString();
        }

如果列表框包含:1,2,3,4
示例文本框内的输出:1,1,1,1
预期输出:1,2,3,4(对于evry选择,它不应再次显示已选择的值)

if listbox contain :1,2,3,4 example output inside textbox: 1,1,1,1 expected output: 1,2,3,4 (for evry selection it shouldnt display the already selected value again)

推荐答案

var selectedItemText =   new List<string>();
    foreach (var li in ListBox1.Items)
    {
       if (li.Selected == true)
        {
         selectedItemText.Add(li.Text);
        }
    }

然后

var result =   string.Join(selectedItemText,",");

这篇关于如何在asp.net列表框中获取多个选定项并在文本框中显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆