Where we took two ListBoxes and their list item, we will put that list item inito each other list box.

Initial chamber

Step 1

Open your Visual Studio 2010 and create an Empty Website, provide a suitable name (listbox_demo).

Step 2

In Solution Explorer you get your empty website, then add two web forms and a SQL Server database as in the following.

For Web Form:

listbox_demo (your empty website) then right-click then select Add New Item -> Web Form. Name it listbox_demo.aspx.

Design chamber

Step 3

Now open your Listbox_demo.aspx file, where we create our design for our application.

Listbox_demo.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. <style type="text/css">
  7. .style1
  8. {
  9. text-decoration: underline;
  10. font-size: large;
  11. color: #006600;
  12. }
  13. .style5
  14. {
  15. width: 69px;
  16. }
  17. .style6
  18. {
  19. width: 102px;
  20. }
  21. .style7
  22. {
  23. width: 238px;
  24. text-decoration: underline;
  25. color: #0000FF;
  26. }
  27. .style8
  28. {
  29. width: 102px;
  30. text-decoration: underline;
  31. color: #0000FF;
  32. }
  33. .style9
  34. {
  35. width: 238px;
  36. }
  37. .style10
  38. {
  39. width: 4px;
  40. }
  41. .style11
  42. {
  43. width: 4px;
  44. color: #0000FF;
  45. }
  46. </style>
  47. </head>
  48. <body id="nil">
  49. <form id="form1" runat="server">
  50. <div>
  51. <span class="style1"><strong>Move Data from one ListBox to other ListBox</strong></span><br />
  52. <br />
  53. <table style="width: 26%; height: 138px;">
  54. <tr>
  55. <td class="style8">
  56. <strong>ListBox1</strong></td>
  57. <td class="style11">
  58. </td>
  59. <td class="style7">
  60. <strong>ListBox2</strong></td>
  61. </tr>
  62. <tr>
  63. <td class="style6">
  64. <asp:ListBox ID="ListBox1" runat="server" Height="131px" Width="98px"
  65. SelectionMode="Multiple" BackColor="#66FF66">
  66. <asp:ListItem>Cricket</asp:ListItem>
  67. <asp:ListItem>Football</asp:ListItem>
  68. <asp:ListItem>Basketball</asp:ListItem>
  69. <asp:ListItem>Baseball</asp:ListItem>
  70. </asp:ListBox>
  71. </td>
  72. <td class="style10">
  73. <table style="width: 67%; height: 139px;">
  74. <tr>
  75. <td class="style5">
  76. <asp:ImageButton ID="ImageButton1" runat="server"
  77. ImageUrl="~/add 1on 1.jpg" onclick="ImageButton1_Click"
  78. ImageAlign="Bottom" />
  79. </td>
  80. </tr>
  81. <tr>
  82. <td class="style5">
  83. <asp:ImageButton ID="ImageButton2" runat="server"
  84. ImageUrl="~/remove 1 on 1.jpg" onclick="ImageButton2_Click" />
  85. </td>
  86. </tr>
  87. <tr>
  88. <td class="style5">
  89. <asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="~/add all.jpg"
  90. onclick="ImageButton3_Click" />
  91. </td>
  92. </tr>
  93. <tr>
  94. <td class="style5">
  95. <asp:ImageButton ID="ImageButton4" runat="server" ImageUrl="~/remove all.jpg"
  96. onclick="ImageButton4_Click" />
  97. </td>
  98. </tr>
  99. </table>
  100. </td>
  101. <td class="style9">
  102. <asp:ListBox ID="ListBox2" runat="server" Height="131px" Width="98px"
  103. SelectionMode="Multiple" BackColor="#66FF66">
  104. <asp:ListItem>Volleyball</asp:ListItem>
  105. <asp:ListItem>Hockey</asp:ListItem>
  106. <asp:ListItem>Boxing</asp:ListItem>
  107. <asp:ListItem>Tennis</asp:ListItem>
  108. </asp:ListBox>
  109. </td>
  110. </tr>
  111. <tr>
  112. <td class="style6">
  113. </td>
  114. <td class="style10">
  115. <asp:Label ID="lbmsg" runat="server"></asp:Label>
  116. </td>
  117. <td class="style9">
  118. </td>
  119. </tr>
  120. </table>
  121. </div>
  122. </form>
  123. </body>
  124. </html>

Your design will look like this:

design

Code chamber

Step 4

Open your listbox_demo.aspx.cs and write some code so that our application works.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Collections;
  8. public partial class _Default : System.Web.UI.Page
  9. {
  10. ArrayList ar1 = new ArrayList();
  11. ArrayList ar2 = new ArrayList();
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. }
  15. protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
  16. {
  17. if (ListBox1.SelectedIndex >= 0)
  18. {
  19. for (int i = 0; i < ListBox1.Items.Count; i++)
  20. {
  21. if (ListBox1.Items[i].Selected)
  22. {
  23. if (!ar1.Contains(ListBox1.Items[i]))
  24. {
  25. ar1.Add(ListBox1.Items[i]);
  26. }
  27. }
  28. }
  29. for (int i = 0; i < ar1.Count; i++)
  30. {
  31. if (!ListBox2.Items.Contains(((ListItem)ar1[i])))
  32. {
  33. ListBox2.Items.Add(((ListItem)ar1[i]));
  34. }
  35. ListBox1.Items.Remove(((ListItem)ar1[i]));
  36. }
  37. ListBox2.SelectedIndex = -1;
  38. }
  39. else
  40. {
  41. lbmsg.Text = "Please select atleast one listitem";
  42. lbmsg.ForeColor = System.Drawing.Color.Red;
  43. }
  44. }
  45. protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
  46. {
  47. if (ListBox2.SelectedIndex >= 0)
  48. {
  49. for (int i = 0; i < ListBox2.Items.Count; i++)
  50. {
  51. if (ListBox2.Items[i].Selected)
  52. {
  53. if (!ar2.Contains(ListBox2.Items[i]))
  54. {
  55. ar2.Add(ListBox2.Items[i]);
  56. }
  57. }
  58. }
  59. for (int i = 0; i < ar2.Count; i++)
  60. {
  61. if (!ListBox1.Items.Contains(((ListItem)ar2[i])))
  62. {
  63. ListBox1.Items.Add(((ListItem)ar2[i]));
  64. }
  65. ListBox2.Items.Remove(((ListItem)ar2[i]));
  66. }
  67. ListBox1.SelectedIndex = -1;
  68. }
  69. else
  70. {
  71. lbmsg.Text = "Data removed from listbox";
  72. lbmsg.ForeColor = System.Drawing.Color.ForestGreen;
  73. }
  74. }
  75. protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
  76. {
  77. while (ListBox1.Items.Count != 0)
  78. {
  79. for (int i = 0; i < ListBox1.Items.Count; i++)
  80. {
  81. ListBox2.Items.Add(ListBox1.Items[i]);
  82. ListBox1.Items.Remove(ListBox1.Items[i]);
  83. }
  84. }
  85. lbmsg.Text = "All data added to listbox2";
  86. lbmsg.ForeColor = System.Drawing.Color.ForestGreen;
  87. }
  88. protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
  89. {
  90. while (ListBox2.Items.Count != 0)
  91. {
  92. for (int i = 0; i < ListBox2.Items.Count; i++)
  93. {
  94. ListBox1.Items.Add(ListBox2.Items[i]);
  95. ListBox2.Items.Remove(ListBox2.Items[i]);
  96. }
  97. }
  98. lbmsg.Text = "All data removed and moved to listbox1";
  99. lbmsg.ForeColor = System.Drawing.Color.ForestGreen;
  100. }
  101. }
Output chamber

listbox

Here the first button is as:
  1. Move data one.
  2. Remove data one.
  3. Add all.
  4. Remove all.

move data from listbox

move data

I hope you like this. Thank you for reading, have a good day.