Skip to main content

How to populate a CheckBoxList from SqlDataSource in c#

Populate a CheckBoxList from SqlDataSource
CheckBoxList is an web server control that provides a multi selection CheckBox group. CheckBoxList control support data binding. so we can populate CheckBoxList with items from a data source control. To bind CheckBoxList control to a data source, first developers need to create a datasource control such as ObjectDataSource, LinqDataSource, SqlDataSource control etc.

DataSource control contains the items to display in the CheckBoxList. After creating DataSource control, developers can call CheckBoxList DataBind method to bind the DataSource to the CheckBoxList control. This method allow developers to programmatically (dynamically) data bind CheckBoxList control at run time.

CheckBoxList control's DataTextFiled and DataValueFiled properties specify which field in the data source to bind to the Text and Value properties of each list item in CheckBoxList control. ChecBoxList ListItem object contains Text and Value properties. Text property text display in the CheckBoxList control and Value property is associated with ListItem object and hidden in the web browser.

We also can use CheckBoxList DataSourceID property to specify DataSource control using declarative syntax. Declarative syntax also support DataTextField and DataValueField properties.

The following c# example code demonstrate us how can we data bind a CheckBoxList with SqlDataSource control to display CheckBoxList items from database data.

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Clicked(object sender,System.EventArgs e) {
        Label1.Text = "You Selected:<br/>";
        foreach (ListItem li in CheckBoxList1.Items){
            if (li.Selected == true){
                Label1.Text += li.Text + "<br/>";

<html xmlns="">
    <head runat="server">
        <title>CheckBoxList example: how to populate CheckBoxList from SqlDataSource in</title>
        <form id="form1" runat="server">
                <asp:Label ID="Label1" runat="server"></asp:Label>
                <hr />
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AppConnectionString1 %>" SelectCommand="SELECT [ProductName] FROM [Products]">
                <asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="ProductName" RepeatColumns="4" BorderStyle="Solid" BorderColor="Red" BorderWidth="1px">
                <asp:Button ID="Button1" runat="server" Text="Vew Selected Products" OnClick="Button1_Clicked" />
More examples

Popular posts from this blog

How to use Table in

Table Web Server Control ASP.NET Table web server control display a table on a web page. It is a .NET web server control so we can program it in server code such as add, remove table row, cell programmatically. TableRow and TableCell web server controls allow us to display content for the Table. Table server control display tabular data and we can format Table server control using it's built in methods and properties. The big difference between HTML table and Table server control is that we can manipulate Table server control using an object model. Generally we should use Table server control when we intend to add remove rows and cells (columns) programmatically at run time.

Table server control acts as a parent (container) for TableRows controls. Table control's Rows property is a collection of TableRow objects. each TableRow control has a collection named Cells. Cells collection contains TableCell objects.

Actually table server control display the TableCell con…

How to use RadioButton control in c#

RadioButton web server control RadioButton control lets you make a group of radio buttons with other RadioButton. If you set it's GroupName property same for multiple radio buttons then all radio buttons with same name act as a single group. Within a group you can only select one RadioButton at a time. RadioButton group work as like RadioButtonList. With RadioButton control you can more customize it than RadioButtonList items.

Back color, border color, border style, font names, font bold, font italic, fore color (text color) and many more property help you to design RadioButton. It also support CSS class, theme and skin. Even validation are works with RadioButton control. RadioButton have AutoPostBack property and CheckChanged event. By using those feature you can determine immediately which RadioButton is selected from a group.

This example uses a RadioButton group with two RadioButton. Here we assign AutoPostBack property value to true and setup a CheckChanged event. When …

How to use RadioButtonList control in c#

RadioButtonList web server control ASP.NET RadioButtonList control enable user to select an item from list. RadioButtonList support data bind programmatically from database. We can also populate it manually by input list item inside RadioButtonList tag. RadioButtonList is a single selection radio button group. RadioButtonList have an items collection. We can determine which item is selected by test it's SelectedItem property.

We can change RadioButtonList design by setting up it's various property as like BackColor, BorderColor, BorderStyle, BorderWidth, CellPadding, CellSpacing, CssClass, Font-Bold, Font-Italic, Font-Names, Font-Overline, Font-Size etc. there are more three properties RepeatColumns, RepeatDirection and RepeatLayout which help you to place list item vertically or horizontally.

RadioButtonList have standard AutoPostBack property. As like other list control RadioButtonList have an excellent event SelectedIndexChanged. So that when someone select an item it…