How to data bind ListBox programmatically in asp.net c#

Data bind ListBox programmatically
ListBox is an ASP.NET list web server control. ListBox control's items collection contains ListItem objects. We can populate a ListBox with items by declarative syntax. We can place the ListItem element between the opening and closing tags of the ListBox control to display it in the browser.

ListBox server control supports data binding. To bind the ListBox control to a data source control, developers need to first create a data source such as a DataSourceControl object. DataSourceControl serves as the base class for controls that represent data sources to data-bound controls. The DataSourceControl object contains the items to display in the ListBox.

Next developers can call the ListBox DataBind method to bind the data source to the ListBox. ListBox DataTextFiled and DataValueField properties specify which field in the data source to bind the Text and Value properties. After data binding, ListBox control will display data source data.

The following C# example code demonstrates to us how can we data-bind ListBox control with data source programmatically at run time. This code creates a String array data source and binds data with ListBox control. ListBox displays array elements as list items.
ListBoxDataBind.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        // Initialize a new array
        string[] controls = {
            "Login",
            "ValidationSummary",
            "RegularExpressionValidator",
            "DataList", "GridView"
        };

        // Specify the ListBox data source
        ListBox1.DataSource = controls;

        // Finally, data bind the ListBox
        ListBox1.DataBind();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to data bind ListBox programmatically</title>
</head>
<body style="padding:25px">
    <form id="form1" runat="server">
    <div>
        <h2 style="color:MidnightBlue; font-style:italic;">      
            Data bind ListBox programmatically
        </h2>      
        <hr width="450" align="left" color="Gainsboro" />
        <asp:Label
            ID="Label1"
            runat="server"
            Text="ASP.NET Controls"
            Font-Bold="true"
            ForeColor="Navy"
            Font-Size="Large"
            >
        </asp:Label>
        <br />
        <asp:ListBox
            ID="ListBox1"
            runat="server"
            AutoPostBack="false"
            BackColor="Tomato"
            ForeColor="FloralWhite"
            Font-Names="Comic Sans MS"
            Font-Size="X-Large"
            />
        <br /><br />
        <asp:Button
            ID="Button1"
            runat="server"
            Text="DataBind ListBox"
            Font-Bold="true"
            ForeColor="DodgerBlue"
            Height="45"
            Width="150"
            OnClick="Button1_Click"
            />
    </div>
    </form>
</body>
</html>