国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

對ListBox的添加移除操作實例分享

2024-07-10 13:16:54
字體:
來源:轉載
供稿:網友
前臺代碼:

復制代碼 代碼如下:


<div>
<asp:ListBox runat="server" 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 runat="server" Text="添加" />
 
<asp:Button runat="server" Text="移除" />
 <asp:ListBox runat="server" SelectionMode="Multiple"></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);
}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天长市| 牙克石市| 岳普湖县| 德清县| 福海县| 墨玉县| 弋阳县| 红安县| 冀州市| 高青县| 平乐县| 安新县| 通道| 社旗县| 上虞市| 改则县| 如东县| 色达县| 丹江口市| 白玉县| 曲麻莱县| 大余县| 桐梓县| 石首市| 水城县| 壤塘县| 德州市| 龙门县| 巧家县| 九龙坡区| 阆中市| 南华县| 莲花县| 东辽县| 民乐县| 绥棱县| 张家港市| 五河县| 昌黎县| 大英县| 招远市|