我有以下问题,我可以从两个列表框中的每一个中选择一个项目并将它们放在另一个列表框中,但是当我选择多于1时,它只连接第一个。 我想添加能够在每个列表框中选择多个的功能,并将所有选定的项目从第一个框中放入第二个框中的每个项目,并显示所有组合第三个。
我已经将我的代码包含在我所拥有的基本功能中。 谢谢你的帮助!
这是我的aspx页面
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
<asp:ListItem>Item1</asp:ListItem>
<asp:ListItem>Item2</asp:ListItem>
<asp:ListItem>Item3</asp:ListItem>
<asp:ListItem>Item4</asp:ListItem>
</asp:ListBox>
<asp:ListBox ID="ListBox2" runat="server" SelectionMode="Multiple">
<asp:ListItem>ListItem1</asp:ListItem>
<asp:ListItem>ListItem2</asp:ListItem>
<asp:ListItem>ListItem3</asp:ListItem>
</asp:ListBox>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Connect" />
<br />
<asp:ListBox ID="ListBoxResult" runat="server"></asp:ListBox>
</div>
</form>
</body>
</html>
这是我的cs页面
namespace SAM_Phase3
{
public partial class DoubleListBoxMM : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ListBoxResult.Items.Add(ListBox1.SelectedItem.Text + ListBox2.SelectedItem.Text);
}
}
}
如果选择了所有项目,我希望获得的输出,将是具有以下项目的第三个文本框。
Item1ListItem1
Item1ListItem2
Item1ListItem3
Item2ListItem1
Item2ListItem2
Item2ListItem3
Item3ListItem1
Item3ListItem2
Item3ListItem3
答案 0 :(得分:1)
实际上,根据您发布的预期结果,这可能就是您想要的......
ListItem newItem = null;
foreach (ListItem item in ListBox1.Items)
{
if (item.Selected)
{
foreach (ListItem innerItem in ListBox2.Items)
{
if (innerItem.Selected)
{
newItem = new ListItem();
newItem.Text = item.Text + innerItem.Text;
ListBoxResult.Items.Add(newItem);
}
}
}
}