HTML:
- <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
- <asp:ListItem>a</asp:ListItem>
- <asp:ListItem>b</asp:ListItem>
- <asp:ListItem>c</asp:ListItem>
- </asp:ListBox>
- <asp:Button ID="Button1" runat="server" Text="Add" onclick="Button1_Click" />
- <asp:ListBox ID="ListBox2" runat="server" SelectionMode="Multiple">
- </asp:ListBox>
- <asp:Button ID="Button2" runat="server" Text="Del" onclick="Button2_Click" />
C#:
- protected void Button1_Click(object sender, EventArgs e)
- {
- //單選add時用下列這行
- //ListBox2.Items.Add(ListBox1.SelectedItem);
- //多選add用迴圈
- for (int i = 0; i < ListBox1.Items.Count; i++)
- {
- if (ListBox1.Items[i].Selected)
- {
- ListBox2.Items.Add(ListBox1.Items[i]);
- }
- }
- }
- protected void Button2_Click(object sender, EventArgs e)
- {
- //單選del
- //ListBox2.Items.RemoveAt(ListBox2.SelectedIndex);
- //多選del用迴圈
- for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
- {
- if (ListBox2.Items[i].Selected)
- {
- ListBox2.Items.RemoveAt(i);
- }
- }
- }
沒有留言:
張貼留言