我正在尝试将列表框中的所有选定项目显示到文本框中。目前我正在做以下事情但没有成功:
For i As Integer = 0 To lb_words.ListCount
If lb_words.Selected(i) = True Then
tb_text.Text &= " Presto"
End If
Next
应该发生的事情是,对于我的列表框中的每个选定项目(lb.words),我希望将它附加到我的文本框中。所以说我的列表框包含Apple,Orange和Banana,我选择Apple和Banana,我的文本框文本应该是“Apple Banana”...
我刚刚向ASP.NET介绍了自己,所以请保持简单:D谢谢。
答案 0 :(得分:3)
试试这个:
Dim s as String = ""
For each x as ListItem in lb_words.Items
if x.Selected Then s &= x.Text & " "
Next
答案 1 :(得分:3)
foreach (ListItem Mail in ListBox1.Items)
{
if (Mail.Selected)
{
Mail.Selected = true+",";
mail.To.Add(Mail.ToString());
}
答案 2 :(得分:1)
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>
代码隐藏(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();
答案 3 :(得分:1)
如果您正在使用数据绑定列表框,请使用按钮点击事件尝试此代码:
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
答案 4 :(得分:0)
一种解决方案是覆盖.ToString()方法以连接列表中的所有值。
答案 5 :(得分:0)
您可以使用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()
如果您选择Apple&amp;香蕉,你的文本框将包含'Apple Banana'
答案 6 :(得分:0)
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;
}
}