visual-studio ASP.NET 从文本框中的列表框中显示所选项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/873944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:29:49  来源:igfitidea点击:

ASP.NET Display Selected Items from Listbox in Textbox

asp.netvisual-studiotextboxlistboxloops

提问by

I'm trying to display all selected items from a listbox into a textbox. Currently I'm doing the following without success:

我正在尝试将列表框中的所有选定项目显示到文本框中。目前我正在执行以下操作但没有成功:

For i As Integer = 0 To lb_words.ListCount
    If lb_words.Selected(i) = True Then
        tb_text.Text &= " Presto"
    End If
Next

What should be happening is that for every selected item in my listbox (lb.words) I want it to be appended to my textbox. So say my listbox contains Apple, Orange and Banana and I select Apple and Banana, my textbox text should read "Apple Banana"...

应该发生的是,对于我的列表框 (lb.words) 中的每个选定项目,我希望将其附加到我的文本框中。所以说我的列表框包含 Apple、Orange 和 Banana,我选择 Apple 和 Banana,我的文本框文本应该显示为“Apple Banana”...

I've just introduced myself to ASP.NET so keep things simple :D Thanks.

我刚刚向 ASP.NET 介绍了自己,所以保持简单:D 谢谢。

采纳答案by Stephen Wrighton

Try this:

试试这个:

Dim s as String = ""

For each x as ListItem in lb_words.Items
     if x.Selected Then s &= x.Text & " "
Next

回答by Rock

        foreach (ListItem Mail in ListBox1.Items)
        {
            if (Mail.Selected)
            {
                Mail.Selected = true+",";
                mail.To.Add(Mail.ToString());                                   
            }

回答by RAJ

If you are using databound listbox then try this code using button click event:

如果您使用数据绑定列表框,请使用按钮单击事件尝试此代码:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    For Each objDataRowView As DataRowView In Me.ListBox1.SelectedItems
        Me.TextBox1.Text &= (objDataRowView("ITEM LIST").ToString() & " ; ")
        'here "ITEM LIST" is the "column name" which is used as DATASOURCE for LISTBOX1
    Next
End Sub

回答by TheVillageIdiot

aspx page:

aspx页面:

   <asp:ListBox ID="myList" runat="server" SelectionMode="Multiple">
        <asp:ListItem>Apple</asp:ListItem>
        <asp:ListItem>Orange</asp:ListItem>
        <asp:ListItem>Grapes</asp:ListItem>
   </asp:ListBox>
   <br/>
   <asp:TextBox id="myText" runat="server"></asp:TextBox>

codebehind (C#)

代码隐藏(C#)

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

回答by SO User

You can use lb_words.SelectedItems instead of looping through all the records and finding the selected items among them. You can use the following code:

您可以使用 lb_words.SelectedItems 而不是遍历所有记录并在其中查找所选项目。您可以使用以下代码:

        Dim s As New StringBuilder()

        For Each item As Object In Me.lb_words.SelectedItems
            s.Append(item)
            s.Append(" ")
        Next

        Me.TextBox1.Text = s.ToString()

If you select Apple & Banana, your textbox will contain 'Apple Banana '

如果您选择 Apple & Banana,您的文本框将包含“Apple Banana”

回答by Wayne Hartman

One solution would be to override the .ToString() method to concatenate all the values in your list.

一种解决方案是覆盖 .ToString() 方法以连接列表中的所有值。

回答by Malak

 protected void Button1_Click(object sender, EventArgs e)
{
    //to display multiple items
    String it =" ";
    foreach (ListItem item in listbox.Items)
    {
        if (item.Selected)
        {
            it = it + ", " + item.Text;
        }
        Label1.Text = it;
    }

}