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 ListItem element between opening and closing tags of listbox control to display it in browser.

listbox server control supports data binding. to bind the listbox control to a data source control, developers need to first create data source such as a DataSourceControl object. DataSourceControl serves as the base class for controls that represent data sources to data-bound controls. 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 filed 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 demonstrate us how can we data bind listbox control with data source programmatically at run time. this code create a String array data source and bind data with listbox control. listbox display 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>
More asp.net examples