asp.net - Populate a DropDownList from a two dimensional array

Populate a DropDownList from two dimensional array
TwoDimensionArrayDropDownList.aspx

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

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        string[,] favoriteColors = { { "DarkGoldenRod", "#B8860B" }, { "DarkGray", "#A9A9A9" }, { "DarkGreen", "#006400" }, { "DarkKhaki", "#BDB76B" }, { "DarkMagenta", "#8B008B" } };
        Label1.Text = "Two dimensional String array created successfully!<br />and populates the DropDownList:<br /><br />";
        int rows = favoriteColors.GetUpperBound(0);
        int columns = favoriteColors.GetUpperBound(1);
        DropDownList1.Items.Clear();
        Label2.Text = "";
        
        for (int currentRow = 0; currentRow <= rows; currentRow++)
        {
            ListItem li = new ListItem();
            for (int currentColumn = 0; currentColumn <= columns; currentColumn++)
            {
                if (currentColumn == 0)
                {
                    li.Text = favoriteColors[currentRow, currentColumn];
                }
                else
                {
                    li.Value = favoriteColors[currentRow, currentColumn];
                }
            }
            DropDownList1.Items.Add(li);
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        Label2.Text = "You selected:";
        Label2.Text += "<br />Item Text: " + DropDownList1.SelectedItem.Text;
        Label2.Text += "<br />Item Value: " + DropDownList1.SelectedItem.Value;
    
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to populate DropDownList from two dimension string array in asp.net</title>
</head> 
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Red">asp.net array example:<br />Two Dimension String Array And DropDownList</h2>
        <asp:Label 
             ID="Label1" 
             runat="server" 
             Font-Size="Large"
             ForeColor="SeaGreen"
             Font-Bold="true"
             Font-Italic="true"
             >
        </asp:Label>
        <asp:Label 
             ID="Label2" 
             runat="server" 
             Font-Size="Large"
             ForeColor="DodgerBlue"
             Font-Bold="true"
             Font-Italic="true"
             >
        </asp:Label>
        <br /><br />
        <asp:DropDownList 
             ID="DropDownList1" 
             runat="server"
             BackColor="Crimson"
             ForeColor="AliceBlue"
             AutoPostBack="true"
             OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
             >
        </asp:DropDownList>
        <br /><br />
        <asp:Button 
             ID="Button1" 
             runat="server" 
             OnClick="Button1_Click"
             Font-Bold="true"
             Text="Populate DropDownList With Two Dimensional Array"
             ForeColor="DodgerBlue"
             />   
    </div>
    </form>
</body>
</html>












More asp.net examples