- Populate a CheckBoxList from an XmlDataSource

CheckBoxList example: how to populate CheckBoxList from XmlDataSource
Here I present simple example of CheckBoxList: How can we populate CheckBoxList with XmlDataSource.

First we need to create a simple Xml file name DataToolBoxControls.xml file. This file should place in App_Data folder. Here is the source code of DataToolBoxControls.xml file.

<?xml version="1.0" encoding="utf-8" ?>
  <Control Name="ListView" />
  <Control Name="GridView" />
  <Control Name="DetailsView" />
  <Control Name="FormView" />
  <Control Name="Repeater" />
  <Control Name="DataList" />
  <Control Name="LinqDataSource" />
  <Control Name="EntityDataSource" />
  <Control Name="SqlDataSource" />
  <Control Name="AccessDataSource" />
  <Control Name="ObjectDataSource" />
  <Control Name="XmlDataSource" />
  <Control Name="SiteMapDataSource" />

Generate CheckBoxList with XmlDataSource
Now create a Web Form name CheckBoxListXmlDataSource.aspx. Then place an XmlDataSource control, a CheckboxList Control, a Button Control and two additional Label control. The XmlDataSource control's DataFile is DataToolBoxControls.xml file. Now bind the XmlDataSource with CheckBoxList controls. The theme is when someone click the Button the Label control shows his selection. Here is the source code of CheckBoxListXmlDataSource.aspx file.

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e) {
        Label1.Text = "Your favorite DataToolBox Control(s): <br />";
            foreach(ListItem li in CheckBoxList1.Items){
                if(li.Selected == true){
                    Label1.Text += li.Text.ToString() + "<br />";

<html xmlns="">
<head runat="server">
    <title>CheckBoxList XmlDataSource Simple Example</title>
    <form id="form1" runat="server">
        <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/DataToolBoxControls.xml">
        <asp:Label ID="Label1" runat="server" />
        <br /><br />
        <asp:Label ID="Label2" runat="server" Text="Select your favorite DataToolBox Control(s)"></asp:Label>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Show Selection" OnClick="Button1_Click" />
More examples