asp.net - How to use RadioButtonList DataBound event

RadioButtonList DataBound event
The following asp.net c# example code demonstrate us how can we perform some task on RadioButtonList control's DataBound event. RadioButtonList is an asp.net list web server control. RadioButtonList control render a group of radio button controls. Each ListItem object of RadioButtonList control generate a radio button control with same group name.

RadioButtonList control's DataBound event occurs after the RadioButtonList control binds to a data source. DataBound event notify the RadioButtonList control that the data binding has completed. So we can perform some task on DataBound event.

In this tutorial, we design and style a Label control on RadioButtonList DataBound event. We also set the RadioButtonList control's first item as selected item in its DataBound event (after data binding complete). We can perform any other action to RadioButtonList control's items after data binding completed (on DataBound event).
RadioButtonListOnDataBoundEvent.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        string[] colors = { "Thistle", "Tomato", "Salmon", "Sienna","WhiteSmoke" };
        RadioButtonList1.DataSource = colors;
        RadioButtonList1.DataBind();
    }

    protected void RadioButtonList1_DataBound(object sender, EventArgs e)
    {
        Label1.Text ="Pick your favorite color: ";
        Label1.ForeColor = System.Drawing.Color.OrangeRed;
        Label1.Font.Name = "Ms Sans Serif";
        Label1.Font.Italic = true;
        Label1.Font.Underline = true;
        RadioButtonList1.Items[0].Selected = true;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to use RadioButtonList OnDataBound event in asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:SeaGreen; font-style:italic;">
            How to use RadioButtonList
            <br /> OnDataBound event in asp.net
        </h2>
        <hr width="350" align="left" color="DarkSeaGreen" />
        <asp:Label 
            ID="Label1" 
            runat="server"
            >
        </asp:Label>
        <asp:RadioButtonList
             ID="RadioButtonList1"
             runat="server"
             RepeatLayout="OrderedList"
             Font-Names="Comic Sans MS"
             Font-Size="Medium"
             OnDataBound="RadioButtonList1_DataBound"
             >
        </asp:RadioButtonList>
        <br />
        <asp:Button 
            ID="Button1"
            runat="server"
            OnClick="Button1_Click"
            Text="Populate RadioButtonList"
            Height="45"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            />
    </div>
    </form>
</body>
</html>
More asp.net examples