Bind CheckBoxList And Get The Selected Items In ASP.NET C#

In this article we will learn how to Bind Chekbox List and get the Selected Items from List. We will make a Web application using C#. Let’s start.

  1. Open Visual Studio. Select New Web Site.



  2. Name it as you want. My application's name is CheckBoxList_Example.


  3. First Of All Add Web Form. Go to Solution Explorer > Right Click on the Web Site > Select Add > Add New Item > Select > Web Form and Give the Web Form Name, My We Form Page Name Is CheckList_Page.


  4. Add Check Box List , Label and Button On CheckList_Page.aspx Page.
    1. <asp:CheckBoxList ID="CheckboxList1" runat="server" AutoPostBack="false" Width="450px">   
    2. </asp:CheckBoxList>  
    3.   
    4. <asp:Label ID="lblmsg" class="textcls" runat="server"></asp:Label>  
    5.   
    6. <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Show Data" />  
  5. Create a method for Bind the CheckBox List.
    1. protected void Bind_CheckList() // Method for Binding The Checkbox List  
    2.  {  
    3.      List<string> listContents = new List<string>();// Create a List of String Elements  
    4.      listContents.Add("DehraDun");  
    5.      listContents.Add("Shimla");  
    6.      listContents.Add("Patna");  
    7.      listContents.Add("Mumbai");  
    8.      listContents.Add("Delhi");  
    9.      listContents.Add("Goa");  
    10.      listContents.Add("Chennai");  
    11.      listContents.Add("Noida");  
    12.      listContents.Add("GuruGram");  
    13.   
    14.      CheckboxList1.DataSource = listContents;//Set Datasource to CheckBox List  
    15.      CheckboxList1.DataBind(); // Bind the checkboxList with String List.  
    16.   
    17.  }  
  6. Call this Method On PageLoad.
    1. protected void Page_Load(object sender, EventArgs e)  
    2.     {  
    3.         if (Page.IsPostBack == false)  
    4.         {  
    5.             Bind_CheckList();// Call CheckBox List Bind Method.  
    6.         }         
    7.     }  
  7. Get the selected Item List From CheckBoxList Items and show Selected Items List with Help of Label .
    1. protected void Button1_Click(object sender, EventArgs e)  
    2.   {  
    3.       string str = "";  
    4.   
    5.       for(int i=0;i<CheckboxList1.Items.Count ;i++)  
    6.       {  
    7.           if(CheckboxList1.Items[i].Selected==true)// getting selected value from CheckBox List  
    8.           {  
    9.                str += CheckboxList1.Items[i].Text  + " ," + "<br/>" ; // add selected Item text to the String .  
    10.           }  
    11.            
    12.         
    13.       }  
    14.       if(str!="")  
    15.       {  
    16.           str = str.Substring(0, str.Length - 7); // Remove Last "," from the string .  
    17.           lblmsg.Text = "Selected Cities are <br/><br/>" + str; // Show selected Item List by Label.  
    18.       }  
    19.   }  

I hope this is helpful for developing. Thanks.