asp.net listbox多选并在lable中显示
时间: 2023-07-15 10:14:20 浏览: 190
可以通过以下步骤实现:
1. 在 ASP.NET 页面中添加一个 ListBox 控件和一个 Label 控件:
```html
<asp:ListBox ID="myListBox" runat="server" SelectionMode="Multiple">
<asp:ListItem Text="Option 1" Value="1"></asp:ListItem>
<asp:ListItem Text="Option 2" Value="2"></asp:ListItem>
<asp:ListItem Text="Option 3" Value="3"></asp:ListItem>
</asp:ListBox>
<asp:Label ID="myLabel" runat="server"></asp:Label>
```
2. 在代码中,处理 ListBox 的 SelectedIndexChanged 事件,并将选中的项显示到 Label 中:
```csharp
protected void myListBox_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedItems = "";
foreach (ListItem item in myListBox.Items)
{
if (item.Selected)
{
selectedItems += item.Text + ", ";
}
}
// 去掉最后一个逗号
selectedItems = selectedItems.TrimEnd(',', ' ');
myLabel.Text = "你选择了:" + selectedItems;
}
```
这样,当用户在 ListBox 中选择多个项时,它们会显示在 Label 控件中。
阅读全文
相关推荐

















