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

首頁(yè) > 編程 > .NET > 正文

對(duì)ListBox的添加移除操作實(shí)例分享

2024-07-10 12:45:53
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
前臺(tái)代碼:
代碼如下:
<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>

后臺(tái)代碼:
代碼如下:
protected void btnAdd_Click(object sender, EventArgs e)
{
#region listbox添加記錄的一種錯(cuò)誤理解
//選擇多條記錄的時(shí)候,會(huì)有一條沒(méi)有被添加,這是因?yàn)楫?dāng)一條記錄被移除后,原來(lái)的第二條記錄的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索引號(hào)進(jìn)行添加的簡(jiǎn)單寫(xiě)法
//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);
}
}
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 南丰县| 桑植县| 嘉兴市| 葫芦岛市| 西乡县| 田阳县| 裕民县| 汽车| 全椒县| 永城市| 林芝县| 盖州市| 巴彦县| 新泰市| 盱眙县| 诸暨市| 沙坪坝区| 淮安市| 夏邑县| 高台县| 宁南县| 铁力市| 江山市| 广州市| 蓝田县| 安义县| 尼玛县| 朝阳区| 临邑县| 镇雄县| 阳东县| 防城港市| 珠海市| 和林格尔县| 临潭县| 临颍县| 策勒县| 普兰店市| 桐城市| 虎林市| 报价|