Source code location: ./composite_data_bound_control/booklistcontrol.cs

using System.Web.UI.WebControls;
using System.Collections;
using System.Web.UI.HtmlControls;
using System.Web;
using System;

namespace anrControls
{
    public class BookListControl : CompositeDataBoundControl
    {
        protected override int CreateChildControls (IEnumerable dataSource, bool dataBinding)
        {
            int itemCount = 0;

            if (dataSource != null)
            {
                /* It's important to add the bookList control at this point so it
                 * can track view state of its items. */
                CheckBoxList bookList = new CheckBoxList ();
                Controls.Add (bookList);

                IEnumerator e = dataSource.GetEnumerator ();

                while (e.MoveNext ())
                {
                    ListItem bookItem = new ListItem ();

                    /* If the control is rebuilding itself with data from 
                     * view state, dataBinding is false and dataSource contains an array 
                     * of null elements. */
                    if (dataBinding)
                    {
                        Book book = (Book) e.Current;
                        bookItem.Text = string.Format ("{0}: {1:C}", book.Title, book.Price);
                    }

                    bookList.Items.Add (bookItem);
                    itemCount++;
                }
            }

            return itemCount;
        }
    }
}