How To Move CheckBoxList To ListBox In ASP.NET Using C#

In this tutorial, we will learn about checkboxlist, and listbox, and how to move the selected data in checkboxlist to the Listbox. Here, we are not taking dynamic data to the checkbox but we are creating a list item in the checkboxlist and working around that.

INITIAL CHAMBER

Step 1

Open your Visual Studio 2010 and create an Empty Website. Give a suitable name [checkboxlist_demo].

Step 2

In Solution Explorer, you get your empty website. Add a web form like this –

For Web Form

checkboxlist _demo (Your Empty Website) -> Right click -> Add New Item -> Web Form. Name it as -> checkboxlist _demo.aspx.

DESIGN CHAMBER

Step 3

Now Open your checkboxlist _demo.aspx file, where we create our design for our simple demo, we take checkboxlist, ListBox and a Label from the toolbox. Here in the Checkboxlist add ListItem by clicking on the smart tag. You can add any data to listitem, for the simple convenience we had taken favorites sports data.

The approach is very simple, when you select any checkbox, it will transfer that selected data to the listbox, it will also show in the label according to count measure we put in the checkboxlist.

checkboxlist _demo.aspx 

c#

  1. <tr>  
  2.                     <td class="auto-style1">  
  3.                         <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">  
  4.                             <asp:ListItem>Cricket</asp:ListItem>  
  5.                             <asp:ListItem>Football</asp:ListItem>  
  6.                             <asp:ListItem>Volleyball</asp:ListItem>  
  7.                             <asp:ListItem>Basketball</asp:ListItem>  
  8.                             <asp:ListItem>Hockey</asp:ListItem>  
  9.                         </asp:CheckBoxList>  
  10.                     </td>  
  11.                     <td>  
  12.                         <asp:ListBox ID="ListBox1" runat="server" Height="126px" SelectionMode="Multiple" Width="131px"></asp:ListBox>  
  13.                     </td>  
  14.                     <td> </td>  
  15.                 </tr>   

Here, we have taken one property of Checkboxlist -- > AutopostBack = “True”. It is quite similar to IsPostback in the server side. If you do not include this, then the transfer from checkboxlist to listbox would not happen.

And as we are selecting multiple checkboxes in the checkbolist, it will transfer multiple data to the listbox, and listbox should hold that condition. For that, we have taken listbox property as -> SelectionMode =”Multiple”, by default it is “Single”.

CODE CHAMBER

Step 4

Here, open your checkboxlist_demo.aspx.cs file to write the server side code so that our demo 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.   
  8. public partial class listboxcheckbox : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.   
  13.     }  
  14.   
  15.     protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)  
  16.     {  
  17.         ListBox1.Items.Clear();  
  18.         foreach (ListItem lstitm in CheckBoxList1.Items)  
  19.         {  
  20.             if (lstitm.Selected)  
  21.             {  
  22.                 ListBox1.Items.Add(lstitm.Text);  
  23.             }  
  24.         }  
  25.   
  26.         if (CheckBoxList1.SelectedIndex == -1)  
  27.         {  
  28.             Label1.ForeColor = System.Drawing.Color.Red;  
  29.   
  30.         }  
  31.         else  
  32.         {  
  33.   
  34.             Label1.ForeColor = System.Drawing.Color.Green;  
  35.         }  
  36.   
  37.   
  38.         Label1.Text = " \n You have selected" + "\n" + ListBox1.Items.Count.ToString() + "\n Sports";  
  39.     }  
  40. }  

OUTPUT CHAMBER


Hope you like it. Thank you for Reading. Have a great day.


Similar Articles