首页 新闻 会员 周边

ListBox怎么取多个值,并传入到Gridview中去?

0
悬赏园豆:10 [待解决问题]

这是两个listbox,其中一个listbox1是用户名,另一个listbox2是其他字段,listbox1只能是单选,而listbox2必须是多选,在选的同时把listbox中所选的内容,传到Gridview,该怎么写代码啊?并且在listbox1是单选,listbox2是多选的时候,怎么才能同时实现添加两个数据。

九妹的主页 九妹 | 初学一级 | 园豆:167
提问于:2011-09-14 13:22
< >
分享
所有回答(1)
0

把ListBox的属性设为:SelectionMode=Multiple,可以实现ListBox的多选功能.

要获取这些选中的ListItem,在WinForm下用:

this.listbox2.SelectedItems;

artwl | 园豆:16736 (专家六级) | 2011-09-14 13:31

是在webFrom里面,我是可以添加数据,但是不能同时添加两个数据啊!

支持(0) 反对(0) 九妹 | 园豆:167 (初学一级) | 2011-09-14 13:46

@九妹:

webform下这样获取,至于加到gridview中,你能添加一个,就可以添加两个啊:

ListItemCollection items =new ListItemCollection();
foreach (ListItem item inthis.lst_AXzhz.Items)
{
if (item.Selected)
{
items.Add(item);
}
}
支持(0) 反对(0) artwl | 园豆:16736 (专家六级) | 2011-09-14 13:51

@天行健 自强不息:

View Code
protectedvoid ListBox2_SelectedIndexChanged(object sender, EventArgs e)
{
ListItemCollection items
=new ListItemCollection();
foreach (ListItem item inthis.ListBox2.Items)
{
if (item.Selected)
{
items.Add(item);
}
}
string a ="";
for (int i =0; i < items.Count; i++)
{
a
+= items[i].ToString();

}
user.City
= a.Substring(0, 3);
user.Country
= a.Substring(3, 3);
user.Lot
= a.Substring(7, 4);
user.Code
= a.Substring(11, 3);

}

就是不可以同时添加两条数据

支持(0) 反对(0) 九妹 | 园豆:167 (初学一级) | 2011-09-14 13:57

@九妹:

写了个示例:

前台页面

<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBox1" SelectionMode="Single" runat="server" Height="109px"
Width
="32px">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
</asp:ListBox>
<asp:ListBox ID="ListBox2" SelectionMode="Multiple" runat="server"
Height
="110px" Width="105px">
<asp:ListItem>AA</asp:ListItem>
<asp:ListItem>BB</asp:ListItem>
<asp:ListItem>CC</asp:ListItem>
<asp:ListItem>DD</asp:ListItem>
<asp:ListItem>EE</asp:ListItem>
<asp:ListItem>FF</asp:ListItem>
</asp:ListBox>
<asp:Button ID="Button1" runat="server" Text="添加" onclick="Button1_Click"/>
<br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
</form>

后台代码:

protectedvoid Button1_Click(object sender, EventArgs e)
{
string text1 = ListBox1.SelectedValue;
string text2 ="";
foreach (ListItem item in ListBox2.Items)
{
if (item.Selected)
{
text2
+= item.Value;
}
}
Label1.Text
= text1 +"<br/>"+ text2;
}

效果:

支持(0) 反对(0) artwl | 园豆:16736 (专家六级) | 2011-09-14 15:57
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册