You can also populate the ASP.Net CheckBoxList control items dynamically using C# code. The Items collection property of checkboxlist control provides the access to its Add method that allows you to add new list items programmatically. The Add method has two overloaded versions that accept different types of parameters to insert a new list item. The first type of overloaded Add method accepts the ListItem class object whereas second overloaded method accepts a string type parameter that adds the list item directly at the end of Items collection of checkboxlist control.
In the sample below we have used the first overloaded Add function that accepts the object of ListItem class. For populating the Checkboxlist control using ListItem class object you have to initialize the object using its one of the overloaded constructor. Here we have used the following overloaded constructor of ListItem class to create the list items for checkboxlist control:
new ListItem( string text, string value );
The above C# code generates a list item with its specific text label and back end value attribute. We have also followed the same pattern in the sample below to populate the checkboxlist by specifying the values for text and value properties to add the list items. You can specify the values in the textbox controls and click the button control to add the list item dynamically.
Now try the sample below to understand the functionality to populate the ASP.Net CheckBoxList control items dynamically: