国产探花免费观看_亚洲丰满少妇自慰呻吟_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);
}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 抚远县| 建平县| 尼勒克县| 泌阳县| 桃源县| 东宁县| 汪清县| 山东省| 隆德县| 沁水县| 临邑县| 宣恩县| 泸定县| 湄潭县| 清苑县| 吴川市| 平湖市| 庆元县| 聂拉木县| 巴林右旗| 彩票| 宜宾县| 武乡县| 黔江区| 伊吾县| 元氏县| 赣榆县| 渝北区| 泰安市| 阳西县| 汉源县| 云阳县| 孟津县| 周宁县| 垣曲县| 平原县| 莲花县| 临海市| 金溪县| 鄂伦春自治旗| 芮城县|