Bind the CheckBoxList in ASP.Net Using C#

We will use a bunch of CheckBoxLists and bind them using SQL Server.

Initial Chamber

Step 1

Open Visual Studio 2010 and create an empty website, name it CheckBoxList_demo.

Step 2

In Solution Explorer, you will get your empty website. Add a web form and SQL Database using the following procedure.

For Web Form:

CheckBoxList_demo (your empty website): right-click and select Add New Item Web Form. Name it CheckBoxList_demo.aspx.

For SQL Server Database:

CheckBoxList_demo (your empty website): right-click and select Add New Item SQL Server Database. Add Database inside the App_Data_folder.

Database Chamber

Step 3

In Server Explorer, click on your database Database.mdfTables and  Add New Table. Form a table as in the following:

Table, got tp tbl_data [don't forget to make ID as IS Identity -- True]

ID

table design

This is our data that we will bind to the CheckBoxList and show at run time.

Design Chamber

Step 4

Open your CheckBoxList_demo.aspx file from Solution Explorer and start designing your application.

Here's the code:

  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  
  4.     xmlns="http://www.w3.org/1999/xhtml">  
  5.     <head runat="server">  
  6.         <title></title>  
  7.         <style type="text/css">  
  8.         .style1  
  9.         {  
  10.             text-decoration: underline;  
  11.             font-size: large;  
  12.         }  
  13.         .style2  
  14.         {  
  15.             width: 128px;  
  16.         }  
  17.         .style3  
  18.         {  
  19.             width: 407px;  
  20.         }  
  21.     </style>  
  22.     </head>  
  23.     <body>  
  24.         <form id="form1" runat="server">  
  25.             <div>  
  26.                 <br />  
  27.                 <br />  
  28.                 <br />  
  29.                 <table style="width:100%;">  
  30.                     <tr>  
  31.                         <td class="style2">  
  32.                      </td>  
  33.                         <td class="style3">  
  34.                             <span class="style1">  
  35.                                 <strong>Select your Language</strong>  
  36.                             </span>  
  37.                         </td>  
  38.                         <td></td>  
  39.                     </tr>  
  40.                     <tr>  
  41.                         <td class="style2">  
  42.                      </td>  
  43.                         <td class="style3">  
  44.                      </td>  
  45.                         <td>  
  46.                      </td>  
  47.                     </tr>  
  48.                     <tr>  
  49.                         <td class="style2">  
  50.                      </td>  
  51.                         <td class="style3">  
  52.                             <asp:CheckBoxList ID="CheckBoxList1" runat="server" DataTextField="language"   
  53.             DataValueField="language" AutoPostBack="True" RepeatLayout="OrderedList" Width="432px">  
  54.                                 <asp:ListItem>COBOL</asp:ListItem>  
  55.                                 <asp:ListItem>PERL</asp:ListItem>  
  56.                                 <asp:ListItem>JAVA</asp:ListItem>  
  57.                                 <asp:ListItem>C#</asp:ListItem>  
  58.                                 <asp:ListItem>PHP</asp:ListItem>  
  59.                                 <asp:ListItem>C++</asp:ListItem>  
  60.                             </asp:CheckBoxList>  
  61.                         </td>  
  62.                         <td>  
  63.                             <asp:Label ID="lbmsg" runat="server"></asp:Label>  
  64.                         </td>  
  65.                     </tr>  
  66.                     <tr>  
  67.                         <td class="style2">  
  68.                      </td>  
  69.                         <td class="style3">  
  70.                             <asp:Button ID="Button1" runat="server" onclick="Button1_Click"   
  71.                         Text="Click here to show the Data" />  
  72.                         </td>  
  73.                         <td>  
  74.                      </td>  
  75.                     </tr>  
  76.                 </table>  
  77.                 <br />  
  78.                 <br />  
  79.                 <br />  
  80.                 <p>  
  81.          </p>  
  82.                 <br />  
  83.                 <br />  
  84.             </div>  
  85.         </form>  
  86.     </body>  
  87. </html>  
This is how your design looks.

design looks

Code Chamber

Finally open your CheckBoxList_demo.aspx.cs file to write the code for making the list box like we assumed.

Here's the code:
  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.Data;  
  8. using System.Data.SqlClient;  
  9.   
  10. public partial class _Default : System.Web.UI.Page  
  11. {  
  12.     protected void Page_Load(object sender, EventArgs e)  
  13.     {  
  14.         if (!Page.IsPostBack)  
  15.         {  
  16.             SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");  
  17.             SqlCommand cmd = new SqlCommand("select * from tbl_data", con);  
  18.             SqlDataAdapter sda = new SqlDataAdapter(cmd);  
  19.   
  20.             DataTable dt = new DataTable();  
  21.             sda.Fill(dt);  
  22.             CheckBoxList1.DataSource = dt;  
  23.             CheckBoxList1.DataBind();  
  24.         }  
  25.     }  
  26.     protected void Button1_Click(object sender, EventArgs e)  
  27.     {  
  28.         string k = "";  
  29.         for (int i = 0; i < CheckBoxList1.Items.Count; i++)  
  30.         {  
  31.             if (CheckBoxList1.Items[i].Selected)  
  32.             {  
  33.   
  34.                 k = k + CheckBoxList1.Items[i].Text + "</br>";  
  35.             }  
  36.   
  37.         }  
  38.         lbmsg.Text = k;  
  39.         lbmsg.ForeColor = System.Drawing.Color.ForestGreen;  
  40.    
  41.     }  
  42. }  
Output Chamber

select language



I hope you liked it. Thank you for reading. Have a good day. 

Up Next
    Ebook Download
    View all
    Learn
    View all