How to sort DropDownList items programmatically in asp.net

asp.net dropdownlist sort items
The following asp.net c# example code demonstrate us how can we sort list items of a DropDownList in c# code.

DropDownList class contains no property or method to sorting list items. So we need to apply few techniques to sort a DropDownList items programmatically at run time. At first, we initialize a Generic List with data type ListItem. Next we populate the specified Generic List with DropDownList items. After that we call the Generic List OrderBy() method to sort its items on ascending order.

When we get the sorted Generic List, we remove all items from DropDownList and add items to DropDownList from sorted Generic List. We sort the DropDownList only once when first time page is load. We does not sort DropDownList each time the page is post back.
dropdownlist-sort-items.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>    
      
<!DOCTYPE html>
        
<script runat="server">  
    protected void Page_Load(object sender, EventArgs e)  
    {   
        if(!Page.IsPostBack)
        {
            List<ListItem> list = new List<ListItem>();

            foreach (ListItem li in DropDownList1.Items)
            {
                list.Add(li);
            }

            List<ListItem> sorted = list.OrderBy(b => b.Text).ToList();

            DropDownList1.Items.Clear();
            foreach (ListItem li in sorted)
            {
                DropDownList1.Items.Add(li);
            }
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label1.Text = "you selected....<br />";
        Label1.Text += "item: " + DropDownList1.SelectedItem.Text;
        Label1.Text += "<br />value: " + DropDownList1.SelectedItem.Value;
    }
</script>        
        
<html xmlns="http://www.w3.org/1999/xhtml">        
<head id="Head1" runat="server">        
    <title>asp.net dropdownlist sort items</title>  
</head>        
<body>        
    <form id="form1" runat="server">        
    <div>        
        <h2 style="color:MidnightBlue; font-style:italic;">        
            asp.net example - dropdownlist sort items
        </h2>        
        <hr width="550" align="left" color="Gainsboro" />        
        <asp:Label       
            ID="Label1"       
            runat="server"      
            Text="select an item from dropdownlist."
            Font-Size="X-Large"
            Width="350"
            >      
        </asp:Label>      
        <br /><br />  
        <asp:DropDownList  
            ID="DropDownList1"  
            runat="server"  
            AutoPostBack="true"  
            Width="350"  
            Font-Size="X-Large"  
            CssClass="select"  
            OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
            >  
            <asp:ListItem Text="Pygmy Swift" Value="1"></asp:ListItem>
            <asp:ListItem Text="Chimney Swift" Value="2"></asp:ListItem>
            <asp:ListItem Text="Alpine Swift" Value="3"></asp:ListItem>
            <asp:ListItem Text="Violet Sabrewing" Value="4"></asp:ListItem>
            <asp:ListItem Text="Ecuadorian Hillstar" Value="5"></asp:ListItem>
        </asp:DropDownList>  
    </div>        
    </form>        
</body>        
</html>
More asp.net examples