最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

对ListBox的添加移除操作实例分享

来源:动视网 责编:小采 时间:2020-11-27 22:41:21
文档

对ListBox的添加移除操作实例分享

对ListBox的添加移除操作实例分享:前台代码: 代码如下:<div> <asp:ListBox ID=ListBox1 runat=server Height=123px Width=113px SelectionMode=Multiple> <asp:ListItem>tom</asp:ListItem> <
推荐度:
导读对ListBox的添加移除操作实例分享:前台代码: 代码如下:<div> <asp:ListBox ID=ListBox1 runat=server Height=123px Width=113px SelectionMode=Multiple> <asp:ListItem>tom</asp:ListItem> <


前台代码:
代码如下:

<div>
<asp:ListBox ID="ListBox1" runat="server" Height="123px" Width="113px" SelectionMode="Multiple">
<asp:ListItem>tom</asp:ListItem>
<asp:ListItem>jion</asp:ListItem>
<asp:ListItem>j</asp:ListItem>
<asp:ListItem>l</asp:ListItem>
<asp:ListItem>k</asp:ListItem>
</asp:ListBox>
 <asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="添加" />
 
<asp:Button ID="btnRemove" runat="server" Text="移除" OnClick="btnRemove_Click" />
 <asp:ListBox ID="ListBox2" runat="server" Height="123px" SelectionMode="Multiple" Width="113px"></asp:ListBox>
</div>

后台代码:
代码如下:

protected void btnAdd_Click(object sender, EventArgs e)
{
#region listbox添加记录的一种错误理解
//选择多条记录的时候,会有一条没有被添加,这是因为当一条记录被移除后,原来的第二条记录的index为0
//for (int i = 0; i < ListBox1.Items.Count; i++)
//{
// if (ListBox1.Items[i].Selected == true)
// {
// ListBox2.Items.Add(ListBox1.SelectedValue);
// ListBox1.Items.Remove(ListBox1.SelectedValue);
// }
//}
#endregion
#region listbox利用index索引号进行添加的简单写法
//while (0 <= ListBox1.SelectedIndex)
//{
// ListBox2.Items.Add(ListBox1.SelectedItem);
// ListBox1.Items.Remove(ListBox1.SelectedItem);
//}
#endregion
#region listbox的另一种成功添加方法
List<ListItem> list = new List<ListItem>();
for (int i = ListBox1.Items.Count - 1; i >= 0; i--)
{
if (ListBox1.Items[i].Selected == true)
{
list.Add(ListBox1.Items[i]);
ListBox1.Items.Remove(ListBox1.Items[i]);
}
}
for (int i = 0; i <=list.Count - 1; i++)
{
ListBox2.Items.Add(list[i]);
}
#endregion
}
protected void btnRemove_Click(object sender, EventArgs e)
{
while (0 <= ListBox2.SelectedIndex)
{
ListBox1.Items.Add(ListBox2.SelectedItem);
ListBox2.Items.Remove(ListBox2.SelectedItem);
}
}

文档

对ListBox的添加移除操作实例分享

对ListBox的添加移除操作实例分享:前台代码: 代码如下:<div> <asp:ListBox ID=ListBox1 runat=server Height=123px Width=113px SelectionMode=Multiple> <asp:ListItem>tom</asp:ListItem> <
推荐度:
标签: 添加 移除 listbox
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top