CheckBoxList DataBound event in asp.net c#

CheckBoxList DataBound event
The following asp.net c# example code demonstrate us how can we perform some additional task/action on CheckBoxList DataBound event. CheckBoxList is an asp.net list web server control. CheckBoxList control generate a multi-selection check box group. Each ListItem object of a CheckBoxList render as a CheckBox control in web page.

We can populate a CheckBoxList from various data source object such as Array, Stack, Dictionary, ArrayList, SqlDataSource, ObjectDataSource etc. To data bind a CheckBoxList with a data source object, we can specify the data source and call the DataBind() method.

CheckBoxList control's DataBound event occurs after the CheckBoxList control binds to a data source. CheckBoxList DataBound event inform the control that data binding logic written for this control has completed. So, we can perform any action with CheckBoxList items after data binding with data source object. We can write an event handler for the CheckBoxList DataBound event.

In this tutorial code, we design a label control on CheckBoxList DataBound event. We also specify CheckBoxList's two items are disabled and programmatically check/select one item.
CheckBoxListOnDataBoundEvent.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        string[] colors = {"DarkMagenta","DarkOliveGreen","DimGray","DodgerBlue","Indigo"};
        CheckBoxList1.DataSource = colors;
        CheckBoxList1.DataBind();
    }

    protected void CheckBoxList1_DataBound(object sender, EventArgs e)
    {
        Label1.Text = "Color List";
        Label1.ForeColor = System.Drawing.Color.MediumOrchid;
        Label1.Font.Name = "Comic Sans MS";
        Label1.Font.Underline = true;
        Label1.Font.Size = FontUnit.XLarge;
        CheckBoxList1.Items[1].Enabled = false;
        CheckBoxList1.Items[2].Selected = true;
        CheckBoxList1.Items[3].Enabled = false;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to use CheckBoxList OnDataBound event in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:SlateBlue; font-style:italic;">
            How to use CheckBoxList
            <br /> OnDataBound event in asp.net
        </h2>
        <hr width="350" align="left" color="CadetBlue" />
        <asp:Label 
            ID="Label1"
            runat="server"
            >
        </asp:Label>
        <asp:CheckBoxList
             ID="CheckBoxList1"
             runat="server"
             OnDataBound="CheckBoxList1_DataBound"
             ForeColor="MediumVioletRed"
             >
        </asp:CheckBoxList>
        <br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Populate CheckBoxList"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
More asp.net examples