asp.net - DataBind a RadioButtonList with an array

DataBind a RadioButtonList with array
The following asp.net c# example code demonstrate us how can we data bind a RadioButtonList with an Array data source. RadioButtonList is an asp.net list web server control. RadioButtonList control render a radio group on web page.

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

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 Array object create a new ListItem object when we data bind a list server control with Array data source.

To populate a RadioButtonList with Array data source is very simple in 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 is Array object by using 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>
More asp.net examples