How to dataBind ListBox with array in asp.net c#

DataBind ListBox with array
The following asp.net c# example code demonstrate us how can we data bind a ListBox control with an Array object. ListBox is an asp.net list web server control which allow users to select one or more items at a time based on its SelectionMode property. ListBox control's items are exists as ListItem objects. Each ListItem object have a Text property and optionally a Value property and a Selected property.

We can populate ListBox control's items by data binding with various data source objects such as ObjectDataSource, LinqDataSource, Array, ArrayList, Generic List, Dictionary, SqlDataSource etc. Each element of data source object generate a new ListItem object in ListBox control when we data bind it with ListBox control.

Array is most popular data source object to data bind with list web server control such as BulletedList, RadioButtonList, ListBox etc. In this tutorial, we data bind a ListBox control with an Array object to generate ListBox items dynamically.

To do this, first we initialize an Array object with elements. Next, We specify the Array object as ListBox control's data source by using its DataSource property. Then we call the ListBox control's DataBind() method. ListBox control's DataBind() method data bind the ListBox control with specified data source object. Finally, we get a ListBox control where items are generated dynamically from Array elements.
StringArrayListBox.aspx

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

<!DOCTYPE html>

<script runat="server">

    protected void Button1_Click(object sender, System.EventArgs e)
    {
        string[] controlArray = { "FileUpload", "HiddenField", "HyperLink", "Image", "ImageMap" };
        Label1.Text = "String array created and bind with LisBox successfully!";
        ListBox1.DataSource = controlArray;
        ListBox1.DataBind();
    }
</script>

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