asp.net - DataBind a RadioButtonList with an array

DataBind a RadioButtonList with array
The following ASP.NET C# example code demonstrates to us how can we data bind a RadioButtonList with an Array data source. RadioButtonList is an ASP.NET list web server control. RadioButtonList control renders a radio group on the web page.

RadioButtonList’s each ListItem object renders a single RadioButton with the same group name. We can populate RadioButtonList items from various data sources such as SqlDataSource, ObjectDataSource, Array, ArrayList, Generic List, Dictionary, etc.

The Array is a popular data source object to data bind with a list web server control such as RadioButtonList, BulletedList, ListBox, etc. Each element from the Array object creates a new ListItem object when we data bind a list server control with the Array data source.

To populate a RadioButtonList with an Array data source is very simple in the ASP.NET C# environment. First, we need to create an Array object. Next, we populate the Array object with items/elements. Then we can define the RadioButtonList control's data source to an Array object by using the RadioButtonList control's DataSource property.

Finally, we can call the RadioButtonList control's DataBind() method to populate RadioButtonList items from Array elements.
StringArrayRadioButtonList.aspx

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

<!DOCTYPE html>

<script runat="server">

    protected void Button1_Click(object sender, System.EventArgs e)
    {
        string[] controlArray = { "TextBox", "Label", "ImageMap", "MultiView", "View" };
        Label1.Text = "String array created and bind with RadioButtonList successfully!";
        RadioButtonList1.DataSource = controlArray;
        RadioButtonList1.DataBind();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to populate (DataBind) RadioButtonList using string array DataSource in asp.net</title>
</head> 
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">asp.net array example:<br />String Array and RadioButtonList</h2>
        <asp:Label 
             ID="Label1" 
             runat="server" 
             Font-Size="Large"
             ForeColor="SeaGreen"
             Font-Bold="true"
             Font-Italic="true"
             >
        </asp:Label>
        <br /><br />
        <asp:RadioButtonList 
             ID="RadioButtonList1" 
             runat="server" 
             BackColor="Crimson" 
             ForeColor="Snow"
             RepeatColumns="2"
             >
        </asp:RadioButtonList>
        <br /><br />
        <asp:Button 
             ID="Button1" 
             runat="server" 
             OnClick="Button1_Click"
             Font-Bold="true"
             Text="Populate RadioButtonList With String Array"
             ForeColor="Crimson"
             />   
    </div>
    </form>
</body>
</html>