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
- <tr>
- <td class="auto-style1">
- <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">
- <asp:ListItem>Cricket</asp:ListItem>
- <asp:ListItem>Football</asp:ListItem>
- <asp:ListItem>Volleyball</asp:ListItem>
- <asp:ListItem>Basketball</asp:ListItem>
- <asp:ListItem>Hockey</asp:ListItem>
- </asp:CheckBoxList>
- </td>
- <td>
- <asp:ListBox ID="ListBox1" runat="server" Height="126px" SelectionMode="Multiple" Width="131px"></asp:ListBox>
- </td>
- <td> </td>
- </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.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- public partial class listboxcheckbox : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
-
- }
-
- protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
- {
- ListBox1.Items.Clear();
- foreach (ListItem lstitm in CheckBoxList1.Items)
- {
- if (lstitm.Selected)
- {
- ListBox1.Items.Add(lstitm.Text);
- }
- }
-
- if (CheckBoxList1.SelectedIndex == -1)
- {
- Label1.ForeColor = System.Drawing.Color.Red;
-
- }
- else
- {
-
- Label1.ForeColor = System.Drawing.Color.Green;
- }
-
-
- Label1.Text = " \n You have selected" + "\n" + ListBox1.Items.Count.ToString() + "\n Sports";
- }
- }
OUTPUT CHAMBER
Hope you like it. Thank you for Reading. Have a great day.